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

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

返回一个过滤后的数组ui

  • 用法:
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就返回truethis

  • 用法:
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就返回falsespa

  • 用法
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

找到后返回找到的那一项prototype

  • 用法:
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

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

  • 用法:
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;
}
复制代码
相关文章
相关标签/搜索