Sunny-117 / js-challenges

✨✨✨ Challenge your JavaScript programming limits step by step
https://juejin.cn/column/7244788137410560055
2.03k stars 235 forks source link

Array.prototype.map #11

Open Sunny-117 opened 2 years ago

Sunny-117 commented 2 years ago
const arr = [1, 2, 3]
Array.prototype.map = function (callback) {
    const res = [];
    for (let i = 0; i < this.length; i++) {
        res.push(callback(this[i], i, this))
    }
    return res;
}
const res = arr.map((ele, index, arr) => {
    return ele * 2
})
console.log(res)
CwRv07 commented 2 years ago
Array.prototype._map = function (cb, thisBinding) {
  // 排除回调非函数情况
  if (typeof cb !== "function") {
    throw new TypeError(`${cb} is not a function`);
  }
  // 排除this为非可迭代对象情况
  if (this == null || typeof this[Symbol.iterator] !== "function") {
    throw new TypeError(`${this} is not a iterable`);
  }
  // 将可迭代对象转换成数组
  const array = [...this];
  const result = [];
  // 执行遍历并回调
  for (let i = 0; i < array.length; i++) {
    result.push(cb.call(thisBinding, array[i], i, this));
  }
  return result;
};
veneno-o commented 1 year ago
Array.prototype._map = function (callback, objThis) {
    if (typeof callback !== "function") {
        throw new TypeError("callback type error!");
    }

    const res = [];
    for (let i = 0; i < this.length; ++i) {
        res.push(callback.call(objThis, this[i], i, this));
    }
    return res;
};
sv-98-maxin commented 1 year ago
Array.prototype.myMap = function (cb) {
  const newArr = []
  for (let i = 0, len = this.length; i < len; i++) {
    newArr.push(cb(this[i], i, this))
  }
  return newArr
}
bearki99 commented 1 year ago
Array.prototype.map = function (callback) {
  const res = [];
  for (let i = 0; i < this.length; i++) {
    res.push(callback(this[i], i, this));
  }
  return res;
};
kangkang123269 commented 1 year ago
Array.prototype.map = function(callback, thisArg) {
  var result = [];
  for (var i = 0; i < this.length; i++) {
    result.push(callback.call(thisArg, this[i], i, this));
  }
  return result;
}
LifeIsTerrible commented 1 year ago
Array.prototype._map = function (cb) {
    let res = []
    if (Array.isArray(this)) return
    for (let i = 0; i <= this.length; i++) {
        res.push(cb(this[i], i, this))
    }
    return res
}
YMnotafraid commented 1 year ago
Array.prototype.mymap = function (callback, thisArg) {
  const res = [];
  for (let i = 0; i < this.length; i++) {
    res.push(callback.call(thisArg, this[i], i, this));
    //回调函数默认的this为undefined,thisArg作为回调函数的this
  }
  return res;
};
const arr1 = [1, 2, 3];
const arr2 = [4, 5, 6];
console.log(
  arr1.mymap(function (item, index, arr) {
    return 2 * this[index];
  }, arr2)
); //[ 8, 10, 12 ]
Bbbtt04 commented 1 year ago
Array.prototype.map = function (cb) {
  const res = []
  for (let i = 0; i < this.length; i++) {
    res.push(cb(this[i], i, this))
  }
  return res
}
let arr = [1, 2, 3]
let res = arr.map((item, index, arr) => {
  return item + 1
})
console.log(res);
cay-cospete commented 1 year ago
Array.prototype._map = function(cb) {
  const result = [];
  for(let i=0; i<this.length; i++) {
    result.push(cb(this[i], i, this))
  }
  return result;
}
cscty commented 1 year ago
Array.prototype.Map = function (fn) {
                let res = [];
                this.forEach(function (val, key) {
                    res.push(fn(val, key));
                });
                return res;
            };
JiangXinYu113 commented 3 months ago
Array.prototype.myMap = function (callbackFn, thisArg) {
"use strict";
    if (typeof callbackFn !== "function") {
    throw new Error(callbackFn + " is not a function");
    }
    if (this === null || this === undefined) {
    throw new Error("Array.prototype.map called on null or undefined");
    }

  const arr = Array(this.length || 0);
   for (let i = 0; i < this.length; i++) {
    //判断是否是数组的稀疏项
      if (Object.keys(this).includes(i.toString())) {
        arr[i] = callbackFn.call(thisArg, this[i], i, this);
      }
  }
return arr;
};
jianxingyao commented 2 months ago
Array.prototype.myMap = function(callBack){
    const arr = this;
    const result = [];
    for (let index = 0; index < arr.length; index++) {
        aloneResult = callBack(arr[index], index, arr)
        result.push(aloneResult)
    }
    return result
}
Windseek commented 3 weeks ago

const arr = [1, 2, 3]
Array.prototype.myMap = function(callback) {
  const res = []
  this.forEach((ele, index, arr) => {
    res.push(callback(ele, index, arr))
  })
  return res;
}
const res = arr.myMap((ele, index, arr) => {
    return ele * 2
})
console.log(res)