Javascript Array map(callback)

Description

Javascript Array map(callback)


Array.prototype.map = function(callback){
    const newArray = [];//from  w  w  w . j a  v a 2  s  .  c  o m

    for (let i = 0; i < this.length; i += 1){
        newArray.push(callback(this[i]));
    }
    
    return newArray;
}

Javascript Array map(callback)

// make prototype of map

Array.prototype.map = function(callback) {
  console.log("this", this);
  var result = [];
  for(var i = 0; i < this.length; i++) {
    result.push(callback(this[i]));/*ww w.j  a  va 2s  . c  om*/
  }
  return result;
}

var arr = [1, 2, 3, 4];

var newArr = arr.map(function(item) {
  return item * 2;
});

console.log("newArray", newArr);

Javascript Array map(callback)

var arr = [1,2,3];

Array.prototype.map = function(callback){
   var newArr = [];
   for(var i=0;i<this.length;i++){
       newArr.push(callback(this[i],i,this));
   }/*from www.j  ava  2  s.  com*/
    return newArr;
}
var newArr = arr.map(function(item){
    return item*2;
});

console.log(newArr)

Javascript Array map(callback)

require('./forEach');

Array.prototype.map = function(callback){
    let arr = this;
    let newArr = [];
    arr.forEach(function(item, index, array){
        newArr.push(callback(item, index, array));
    });/*from   w  w  w . j  av a2s  .  c om*/
    return newArr;
}

Javascript Array map(callback)

Array.prototype.map = function(callback) {
  var obj = this;
  var value, mapped_value;
  var A = new Array(obj.length);

  for (var i = 0; i < obj.length; i++) {
    value = obj[i];//from   ww w .j a va 2s  .  c  om
    mapped_value = callback.call(null, value);
    A[i] = mapped_value;
  }

  return A;
};

var arr = [1, 2, 3];
var new_arr = arr.map(function(value) {
  return value * value;
});

console.log(new_arr); // (???) [1, 4, 9]

Javascript Array map(callback)

Array.prototype.map = function(callback){
 var a = 0,//from   w ww  . j a  v a  2  s  .c  o  m
  len = this.length,
  result = [];
 while(a < len){
  result.push(callback(this[a], a++, this));
 }
 return result;
};

Javascript Array map(callback)

Array.prototype.map = function map(callback) {
 if (this === undefined || this === null) {
  throw new TypeError(this + ' is not an object');
 }

 if (!(callback instanceof Function)) {
  throw new TypeError(callback + ' is not a function');
 }

 var//ww w. j  av  a 2s.  com
 object = Object(this),
 scope = arguments[1],
 arraylike = object instanceof String ? object.split('') : object,
 length = Math.max(Math.min(arraylike.length, 9007199254740991), 0) || 0,
 index = -1,
 result = [];

 while (++index < length) {
  if (index in arraylike) {
   result[index] = callback.call(scope, arraylike[index], index, object);
  }
 }

 return result;
};

Javascript Array map(callback)

Array.prototype.map = function(callback){
    const newArr = []/*from  www .  ja v  a  2  s  . c  om*/
    // console.log(this)
    // callback();
    for(let i = 0; i < this.length; i += 1){
        let newValue = callback(this[i])
        // let newValue = callback(this[i],i,this)
        // reference the index and array
        newArr.push(newValue)
    }
    return newArr
}

var someArray = [1,2,3,4]
const returnArray = someArray.map(function(value){
    // const returnArray = someArray.map(function(value, index, array){
    return value
});
console.log(returnArray)

Javascript Array map(callback)

Array.prototype.map = function(callback) {
  for(var i = 0, l = this.length; i < l; i++) {
    this[i] = callback(i, this[i]);/* w  w  w . j  ava2s . c  o  m*/
  }
  return this;
}



PreviousNext

Related