数组常见方法使用以及简易实现

187 阅读2分钟

reduce

方法对数组中的每个元素执行一个由您提供的reducer函数(升序执行),将其结果汇总为单个返回值。

  • 用法:
let r = [1, 2, 3, 4].reduce((a, b, index, arr) => a + b);
console.log(r); // 10
/*
 * 参数:两个(cb,prev)
 * 是否改变原数组:否
 */
  • 简易实现:
Array.prototype.reduce = function (cb,prev) {
  for (let i = 0; i < this.length; i++) {
    if (typeof prev === 'undefined') {
      prev = cb(this[i], this[i + 1], i + 1, this);
      i++;
    } else {
      prev = cb(prev, this[i], i, this);
    }
  }
  return prev;
}

map

返回一个映射后的数组

  • 用法:
let r = [1, 2, 3, 4].map((item, index, array) => {
  return item * 2;
});
console.log(r); // [ 2, 4, 6, 8]
/*
 * 参数:两个(cb,sourceArray)
 * 是否改变原数组:否
 */
  • 简易实现
Array.prototype.map = function (cb) {
  const newArr = [];
  if (typeof cb !== 'function') throw new TypeError(cb + 'is not a function');
  for (let i = 0; i < this.length; i++) {
    newArr[i] = cb(this[i],i,this)
  }
  return newArr;
}

filter

返回一个过滤后的数组

  • 用法:
let r = [1, 2, 3, 2, 4].filter((item, index, array) => {
  return item === 2;
})
console.log(r);//[ 2, 2 ]
/*
 * 参数:两个(cb,sourceArray)
 * 是否改变原数组:否
 */
  • 简易实现
Array.prototype.filter = function (cb) {
  const newArray = [];
  if (typeof cb !== 'function') throw new TypeError(cb + 'is not a function');
  for (let i = 0; i < this.length; i++) {
    if (cb(this[i], i, this)) {
      newArray[newArray.length] = this[i];
    }
  }
  return newArray;
}

some

找到true就返回true

  • 用法:
let r = [1, 2, 3, 4].some((item, index, array) => {
  return item > 2;
})
console.log(r);// true
/*
 * 参数:两个(cb,sourceArray)
 * 是否改变原数组:否
 */
  • 简易实现
Array.prototype.some = function (cb) {
  if (typeof cb !== 'function') throw new TypeError(cb + 'is not a function');
  for (let i = 0; i < this.length; i++) {
    if (cb(this[i], i, this)) {
      return true;
    }  
  }
  return false
}

every

找到false就返回false

  • 用法
let r = [1, 2, 3, 4].every((item, index, array) => {
  return item > 1;
});
console.log(r);// false
/*
 * 参数:两个(cb,sourceArray)
 * 是否改变原数组:否
 */
  • 简易实现
Array.prototype.every = function (cb) {
  if (typeof cb !== 'function') throw new TypeError(cb + 'is not a function');
  for (let i = 0; i < this.length; i++) {
      if (!cb(this[i], i, this)) return false;
  }
  return true;
}

find

找到后返回找到的那一项

  • 用法:
let r = [1, 2, 3, 4, 5, 2].find((item, index,array) => {
  return item > 3;
})
console.log(r);// 4
/*
 * 参数:两个(cb,sourceArray)
 * 是否改变原数组:否
 */
  • 简易实现:
Array.prototype.find = function (cb) {
  if (typeof cb !== 'function') throw new TypeError(cb + 'is not a function');
  for (let i = 0; i < this.length; i++) {
    if (cb(this[i], i, this)) {
      return this[i];
    }    
  }
}

findIndex

找到后返回找到的那一项的索引

  • 用法:
let r = [1, 2, 3, 4].findIndex((item, index, array) => {
  return item > 2;
});
console.log(r);// 2
/*
 * 参数:两个(cb,sourceArray)
 * 是否改变原数组:否
 */
  • 简易实现:
Array.prototype.findIndex = function (cb) {
  if (typeof cb !== 'function') throw new TypeError(cb + 'is not a functions');
  for (let i = 0; i < this.length; i++) {
    if (cb(this[i], i, this)) {
      return i;
    }
  }
  return -1
}

includes

判断是否包含某一项

  • 用法:
let r0 = [1, 2, 3].includes(2);
console.log(r0); // true
let r1 = [1, 2, 3].includes(2,2);
console.log(r1); // false
/*
 * 参数:两个(valueToFind,fromIndex)
 * 是否改变原数组:否
 */
  • 简易实现:
Array.prototype.includes = function (valueToFind, fromIndex) {
  if (fromIndex > this.length) return false;
  for (let i = fromIndex || 0; i < this.length; i++) {
    if (this[i] === valueToFind) return true;
  }
  return false;
}