Array.prototype.forEach = function (cb) {
  for (var i = 0; i < this.length; i++) {
    cb(this[i], i, this);
  }
};

Array.prototype.map = function (cb) {
  var res = [];
  for (var i = 0; i < this.length; i++) {
    res[i] = cb(this[i], i, this);
  }
  return res;
};

Array.prototype.filter = function (cb) {
  var res = [];
  for (var i = 0; i < this.length; i++) {
    if (cb(this[i], i, this)) res.push(this[i]);
  }
  return res;
};

Array.prototype.find = function (cb) {
  for (var i = 0; i < this.length; i++)
    if (cb(this[i], i, this)) return this[i];
  return -1;
};

 

 posted on 2024-01-11 10:50  laremehpe  阅读(2)  评论(0编辑  收藏  举报