Implement array partial methods (map/filter/reduce/some/every/flat)

1. Loop through the array map method

// thisArg 执行 fn 函数时值被用作this
const myMap = function(fn, thisArg) {
    
    
  let arr = Array.prototype.slice.call(this);
  let resultArr = Array();
  for (let i = 0; i < arr.length; i++) {
    
    
    if (!arr.hasOwnProperty(i)) continue;
     //使用call方法来改变this指向
    resultArr[i] = fn.call(thisArg, arr[i], i, this);
  }
  return resultArr;
}

Array.prototype.myMap = myMap;
let arr = [1, 2, 3];
console.log(arr.myMap((item) => item + 1)); // 2,3,4

Note: The array object in js uses the hasOwnProperty method to check whether there is an index in the array,array.hasOwnProperty(index)

const arr = [5, 9, 13];
console.log(arr.hasOwnProperty(0)); // true
console.log(arr.hasOwnProperty(1)); // true
console.log(arr.hasOwnProperty(2)); // true
console.log(arr.hasOwnProperty(3)); // false

2. Loop through the array filter (filter) method

const myFilter = function (fn, thisArg) {
    
    
    let arr = Array.prototype.slice.call(this)
    let newArr = []
    for (let i = 0; i < arr.length; i++) {
    
    
        if(!arr.hasOwnProperty(i)) continue;
         fn.call(thisArg, arr[i], i, this) && newArr.push(arr[i])
    }
    return newArr
}
Array.prototype.myFilter = myFilter
let arr = [1, 2, 3]
console.log(arr.myFilter(item => item === 2)) // [2]

array iterate over some

const mySome = function (fn, context) {
    
    
  let arr = Array.prototype.slice.call(this);
  if (!arr.length) return false;
  for (let i = 0; i < arr.length; i++) {
    
    
    if (!arr.hasOwnProperty(i)) continue;
    let res = fn.call(context, arr[i], i, this);
    if (res) return true;
  }
  return false;
};

Array.prototype.mySome = mySome;

let arr = [1, 2, 3];
console.log(arr.mySome((item) => item === 2));

Note
some: If tested with an empty array, it will return false in any case.
every: If an empty array is received, this method will return true in any case.

Array accumulation reduce


Array.prototype.myReduce = function (fn, initialValue) {
    
    
    let arr = Array.prototype.slice.call(this)
    let startItem
    let startIndex
    if (!initialValue) {
    
    
        for (let i = 0; i < arr.length; i++) {
    
    
            if (!arr.hasOwnProperty(i)) continue
            startIndex = i
            startItem = arr[i]
            break
        }
    } else {
    
    
        startItem = initialValue
    }
    for (let i = ++startIndex || 0; i < arr.length; i++) {
    
    
        if (!arr.hasOwnProperty(i)) continue
        startItem = fn.call(null, startItem, arr[i], i, this)
    }
    return startItem
}

Array.prototype.myReduce = myReduce


let arr = [1, 2, 3]

console.log(arr.myReduce((acc, cur) => acc + cur)) // 6
console.log(arr.reduce((acc, cur) => acc + cur)) // 6

Array flattening flat

/*
** depth 指定要提取嵌套数组的结构深度,默认值为 1。
*/
const myFlat = function (depth = 1) {
    
    
    let arr = Array.prototype.slice.call(this)
    if (depth === 0) return arr
    return arr.reduce((total, current) => {
    
    
        if (Array.isArray(current)) {
    
    
            return [...total, ...myFlat.call(current, depth-1)]
        } else {
    
    
            return [...total, current]
        }
    }, [])
}

Array.prototype.myFlat  = myFlat
let arr = [1, 2, [3, 4, [5, 6,['k','i','t',['e']], 7, 8], 9]]

console.log(arr.myFlat())

Guess you like

Origin blog.csdn.net/qq_44376306/article/details/126590264