手写数组API

1. 数组filter方法,参数传入一个函数,不改变数组本身,不对空数组操作,返回一个新数组。

Array.prototype.filterTest = function (callback) {
  if (typeof callback != 'function') {
    throw Error(`${callback}is not a functuon`)
  }
  const _this = this;
  if (_this.length <= 0) return;
  const arr = [];
  _this.forEach((item, index) => {
    arr.push(callback(item, index, this));
  })
  return arr;
}

const arr = [1, 2, 3, 5];
const result = arr.filterTest((item, index) => { return item == 2 });
console.log(result) //[ 2 ]

2. some方法:不会对空数组进行检测,不改变原数组,如果有一个为true就返回true不检测下边的元素,如果都没有符合条件的则返回false

Array.prototype.someTest = function (callback) {
  if (typeof callback != 'function') {
    throw Error(`${callback}is not a functuon`)
  }
  const len = this.length;
  if (len <= 0) return;
  let result = false;
  for (let i = 0; i < len; i++) {
    if (callback(this[i], i, this)) {
      result = true;
      break;
    }
  }
  return result;
}

const result = arr.someTest((item) => item >= 3);
console.log(result)  //  true

3. every方法:数组所有元素都符合则返回true,如果有一个不符合则返回false并剩余的元素不会进行检测,不会对空数组进行检测,不改变原数组。

Array.prototype.everyTest = function (callback) {
  if (typeof callback != 'function') {
    throw Error(`${callback}is not a functuon`)
  }
  const len = this.length;
  if (len <= 0) return;
  let result = true;
  for (let i = 0; i < len; i++) {
    if (!callback(this[i], i, this)) {
      result = false;
      break;
    }
  }
  return result;
}
const arr = [1, 2, 3, 5];
const result = arr.everyTest((item) => item > 2)
console.log(result) // false

4. map方法: 数组map方法不改变原数组,不对空数组进行检测,返回一个新数组,map的callback需要返回值如果没有return则会是undefined。

Array.prototype.mapTest = function (callback, thisValue) {
  if (typeof callback != 'function') {
    throw Error(`${callback} is not a function`);
  }
  const len = this.length;
  if (len <= 0) return;
  const arr = [];
  this.forEach((item, index) => {
     arr.push(callback.call(thisValue, item, index, this));
  })
  return arr;
}
const arr = [1, 2, 3, 5];
const result = arr.mapTest((item) => item * 2)
console.log(result) // [ 2, 4, 6, 10 ]

 5. reduce方法: 方法接收一个函数作为累加器,数组中的每个值(从左到右)开始缩减,最终计算为一个值,有返回值,不修改原数组。

Array.prototype.reduceTest = function (callback, initTotalVal) {
  if (typeof callback != 'function') {
    throw Error(`${callback} is not a function`);
  }
  const len = this.length;
  if (len <= 0) return;
  let total = initTotalVal;
  for (let i = 0; i < len; i++) {
    if (!initTotalVal && i == 0) {
      total = this[i];
      continue;
    }
    total = callback(total, this[i], i, this);
  }
  return total;
}

const arr = [1, 2, [3], 4, 5];
const result = arr.reduceTest((total, item) => total.concat(item), [])
const arr1 = ['1', '2', '3', '4', '5'];
const result1 = arr1.reduceTest((total, item) => total + item);
console.log(result, result1) // [ 1, 2, 3, 4, 5 ] 12345

6. find方法: 不改变原数组,不执行空数组,如果有一个符合条件的直接返回,不执行下边检测,没有符合条件的返回undefined

Array.prototype.findTest = function (callback) {
  if (typeof callback != 'function') {
    throw Error(`${callback} is not a function`);
  }
  const len = this.length;
  if (len <= 0) return;
  let result;
  for (let i = 0; i < len; i++) {
    if (callback(this[i], i, this)) {
      result = this[i];
      break;
    }
  }
  return result;
}

const arr = [1, 2, 3, 4, 5];
const result = arr.findTest((item) => item > 3)
console.log(result, '>>>>>>>>') // 4

7. findIndex方法:不改变原数组,不执行空数组,如果有一个符合条件的直接返回,不执行下边检测,没有符合条件的返回-1

Array.prototype.findIndexTest = function (callback) {
  if (typeof callback != 'function') {
    throw Error(`${callback} is not a function`);
  }
  const len = this.length;
  if (len <= 0) return;
  let index = -1;
  for (let i = 0; i < len; i++) {
    if (callback(this[i], i, this)) {
      index = i;
      break;
    }
  }
  return index;
}


const arr = [1, 2, 3, 4, 5];
const result = arr.findIndexTest((item) => item > 10)
console.log(result, '>>>>>>>>') // -1

 8. includes方法: 用来判断一个数组是否包含一个指定的值,如果是返回 true,否则false。

Array.prototype.includesTest = function (query, start) {
  const len = this.length;
  if (len <= 0) return;
  let index = 0;
  if (!start && start >= 0) {
    index = len - start - 1;
  }
  let result = false;
  for (let i = 0; i < len; i++) {
    if (this[i] == query || isNaN(query) && isNaN(this[i])) {
      result = true;
      break;
    }
  }
  return result;
}

const arr = [1, 2, 3, 4, NaN];
const result = arr.includesTest(10)
console.log(result, '>>>>>>>>') // false

 9. push方法:在数组的末尾添加一项,返回数据的新长度

Array.prototype.pushTest = function (...param) {
  let len = this.length >>> 0;
  let obj = Object(this);
  let argCount = param.length >>> 0;
  // 2 ^ 53 -1 是JS能表示的最大正整数
  if (len + argCount > 2 ** 53 - 1) {
    throw new TypeError('超出了最大限制');
  }
  for (let i = 0; i < argCount; i++) {
    obj[len + i] = param[i];
  }
  let newLength = len + argCount;
  obj.length = newLength;
  return newLength;
}


const arr = [1, 2, 3, 4, 5];
const result = arr.pushTest(1, 2, 3, 4)
console.log(result, arr, '>>>>>>>>') // 9 [1, 2, 3, 4, 5,1, 2, 3, 4]

 10. pop方法:删除数组的最后一项,返回删除的元素  改变数组

Array.prototype.popTest = function () {
  let len = this.length >>> 0;
  let obj = Object(this);
  if (len === 0) {
    obj.length = 0;
    return undefined;
  }
  len--;
  let value = obj[len];
  delete obj[len];
  obj.length = len;
  return value;
}

const arr = [1, 2, 3, 4, 5];
const result = arr.popTest()
console.log(result, arr, '>>>>>>>>') // 5 [1, 2, 3, 4]
原文地址:https://www.cnblogs.com/7fls/p/15629197.html