简单实现JS的数组方法(更新中)

136 阅读1分钟
isArray,判断参数是否为数组。
//判断是否为数组
Array.prototype._isArray = function (arr){
    return Object.prototype.toString.call(arr) =='[object Array]';
}
cancat,连接多个数组并返回新数组(如果参数不为数组则则将该参数添加到数组末尾)。

image.png

image.png

Array.prototype.mConcat = function () {
    if (arguments.length == 0) {
        return this;
    }
    let res = [];
    for (let i of arguments) {
        if (Array.isArray(i)) {
            res = [...res, ...i];
        } else {
            res.push(i);
        }
    }
    return res;
}
copyWithin,将数组中的数组元素复制到指定位置或从指定位置复制。

分析:会改变原数组内容,不会改变长度,进行的是覆盖操作,在原函数中,如果target不为数字,如果能转为数字则按数字,否则按0算

Array.prototype.mCopyWithin = function (target, start = 0, end = this.length) {
    if (target === undefined) {
        return
    }
    let num = Number(target);
    let _target = num === num ? num : 0;
    let _start = Number(start) === Number(start) ? Number(start) : 0;
    let num2 = Number(end);
    let _end = num2 === num2 ? num2  : 0;
    let arr = [];
    for (let i = _start; i < _end; i++) {
        arr.push(this[i]);
    }
    let len = arr.length;
    let index = 0;
    while (index < len && _target < this.length) {
        this[_target] = arr[index];
        index++;
        _target++;
    }
}
entries

分析:这个函数的返回值与众不同,他会返回一个可被for of遍历的迭代器对象,并且值为 [序号,值] 的形式,所以需要自己新建一个迭代器对象,需要用到新的定义关键词 Symbol,并且为了遍历的进行,不能使用return,需要使用yield,所以函数还为生成器函数。

Array.prototype.mEntries = function () {
    let index = 0;
    let end = this.length;
    let arr = this;
    const iterator = {
        [Symbol.iterator]: function* () {
            while (index < end) {
                yield [index,arr[index]];
                index++;
            }
        }
    }
    return iterator;
}
every

分析:接收一个函数,将数组中的所有元素按照该函数进行判断,所有元素通过返回true,否则返回false。

Array.prototype.mEvery = function (callback) {
    if (typeof (callback) !== 'function') {
        throw new TypeError('callback must be a function');
    }
    for (let i = 0; i < this.length; i++) {
        const isTrue = callback(this[i], i, this);
        if (!isTrue) {
            return false;
        }
        return true;
    }
}
fill 用指定值填充数组
Array.prototype.mFill = function (value, start = 0, end = this.length) {
    for (let i = start; i < end; i++){
        this[i] = value;
    }
}
filter 将符合条件的数返回为一个新数组
Array.prototype.mFilter = function (callback) {
    let len = this.length;
    let res = [];
    for (let index = 0; index < len; index++) {
        const isTrue = callback(this[index], index, this);
        if (isTrue) {
            res.push(this[index]);
        }
    }
    return res;
}
find 查找符合要求的第一个元素,不存在则返回undefined
Array.prototype.mFind = function (callback) {
    let len = this.length;
    if (len === 0) {
        return
    }
    for (let index = 0; index < len; index++) {
        const isTrue = callback(this[index], index, this);
        if (isTrue) return this[index];
        
    }
    return undefined; //可选,当没有return时,效果与return undefined相同。
}
findIndex 查找符合要求的第一个元素的索引,不存在则返回-1
Array.prototype.mFindIndex = function (callback) {
    let len = this.length;
    if (len === 0) {
        return
    }
    for (let index = 0; index < len; index++) {
        const isTrue = callback(this[index], index, this);
        if (isTrue) return index;
    }
    return -1;
}
forEach 遍历数组
Array.prototype.mForEach = function (callback) {
    let len = this.length;
    if (len === 0) {
        return
    }
    for (let index = 0; index < len; index++) {
        callback(this[index], index, this);
    }
}
includes 判断数组中是否包括指定元素,第二个参数为开始搜索位置
Array.prototype.mIncludes = function (element, start = 0) {
    const len = this.length;
    for (let i = start; i < len; i++){
        if (this[i] === element) {
            return true
        }
    }
    return false;
}
indexOf 查找指定元素的位置,存在返回下标,反之返回-1。
Array.prototype.mIndexOf = function (element, start = 0) {
    const len = this.length;
    for (let i = start; i < len; i++){
        if (this[i] === element) {
            return i;
        }
    }
    return -1;
}
join 使用指定字符拼接数组元素并返回字符串。
Array.prototype.mJoin = function (separator = ',') {
    const len = this.length;
    let res = '';
    for (let i = 0; i < len; i++){
        res += this[i].toString() + (i == len - 1 ? '' : separator);
    }
    return res;
}
lastIndexOf
Array.prototype.mLastIndexOf = function (item, start = this.length - 1) {
    for (let i = start; i > -1; i--){
        if (this[i] === item) {
           return start - i 
        }
    }
    return -1;
}