数组方法实现

Array.prototype

Array.prototype.myFlat = function(depth = 1) {
  if (depth < 1) return this;
  return this.reduce((flat, toFlatten) => {
    return flat.concat(Array.isArray(toFlatten) ? toFlatten.myFlat(depth - 1) : toFlatten);
  }, []);
};

// Usage
console.log([1, [2, [3, [4]], 5]].myFlat(2)); // [1, 2, 3, [4], 5]

Array.prototype.myForEach = function(callback, thisArg) {
  for (let i = 0; i < this.length; i++) {
    if (this.hasOwnProperty(i)) {
      callback.call(thisArg, this[i], i, this);
    }
  }
};

// Usage
[1, 2, 3].myForEach((num, index) => console.log(num, index)); // 1 0, 2 1, 3 2

Array.prototype.myMap = function(callback, thisArg) {
  let result = [];
  for (let i = 0; i < this.length; i++) {
    if (this.hasOwnProperty(i)) {
      result.push(callback.call(thisArg, this[i], i, this));
    }
  }
  return result;
};

// Usage
console.log([1, 2, 3].myMap(num => num * 2)); // [2, 4, 6]

Array.prototype.myFilter = function(callback, thisArg) {
  let result = [];
  for (let i = 0; i < this.length; i++) {
    if (this.hasOwnProperty(i) && callback.call(thisArg, this[i], i, this)) {
      result.push(this[i]);
    }
  }
  return result;
};

// Usage
console.log([1, 2, 3].myFilter(num => num > 1)); // [2, 3]
Array.prototype.myReduce = function(callback, initialValue) {
  let accumulator = initialValue === undefined ? this[0] : initialValue;
  let startIndex = initialValue === undefined ? 1 : 0;
  for (let i = startIndex; i < this.length; i++) {
    if (this.hasOwnProperty(i)) {
      accumulator = callback(accumulator, this[i], i, this);
    }
  }
  return accumulator;
};

// Usage
console.log([1, 2, 3].myReduce((acc, num) => acc + num, 0)); // 6

Array.prototype.myFill = function(value, start = 0, end = this.length) {
  start = start < 0 ? Math.max(this.length + start, 0) : Math.min(start, this.length);
  end = end < 0 ? Math.max(this.length + end, 0) : Math.min(end, this.length);
  for (let i = start; i < end; i++) {
    this[i] = value;
  }
  return this;
};

// Usage
console.log([1, 2, 3].myFill(0, 1, 2)); // [1, 0, 3]

Array.prototype.myIncludes = function(value, fromIndex = 0) {
  fromIndex = fromIndex < 0 ? Math.max(this.length + fromIndex, 0) : fromIndex;
  for (let i = fromIndex; i < this.length; i++) {
    if (this[i] === value) {
      return true;
    }
  }
  return false;
};

// Usage
console.log([1, 2, 3].myIncludes(2)); // true

Array.prototype.myPush = function(...args) {
  for (let i = 0; i < args.length; i++) {
    this[this.length] = args[i];
  }
  return this.length;
};

// Usage
let arr = [1, 2, 3];
arr.myPush(4, 5);
console.log(arr); // [1, 2, 3, 4, 5]

Array.prototype.myUnshift = function(...args) {
  this.splice(0, 0, ...args);
  return this.length;
};

// Usage
let arr = [1, 2, 3];
arr.myUnshift(0);
console.log(arr); // [0, 1, 2, 3]

Array.prototype.myCopy = function() {
  return this.slice();
};

// Usage
let arr = [1, 2, 3];
let copy = arr.myCopy();
copy[0] = 0;
console.log(arr); // [1, 2, 3]
console.log(copy); // [0, 2, 3]

Array.prototype.getLevel = function() {
  const getDepth = (arr) => {
    return Array.isArray(arr) ? 1 + Math.max(0, ...arr.map(getDepth)) : 0;
  };
  return getDepth(this);
};

// Usage
console.log([1, [2, [3, [4]]]].getLevel()); // 4

Array.prototype.myIterator = function*() {
  for (let i = 0; i < this.length; i++) {
    yield this[i];
  }
};

// Usage
let arr = [1, 2, 3];
let iterator = arr.myIterator();
console.log(iterator.next().value); // 1
console.log(iterator.next().value); // 2
console.log(iterator.next().value); // 3

Array.prototype.mySort = function(compareFunction) {
  if (compareFunction === undefined) {
    compareFunction = (a, b) => String(a).localeCompare(String(b));
  }
  for (let i = 0; i < this.length - 1; i++) {
    for (let j = i + 1; j < this.length; j++) {
      if (compareFunction(this[i], this[j]) > 0) {
        [this[i], this[j]] = [this[j], this[i]];
      }
    }
  }
  return this;
};

// Usage
let arr = [3, 1, 4, 1, 5, 9];
console.log(arr.mySort()); // [1, 1, 3, 4, 5, 9]

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值