"Implement a Map prototype" with forEach?

So im running into a bit of an issue. I solved this issue (on beta) through:

// the global Array
var s = [23, 65, 98, 5];

Array.prototype.myMap = function(callback){
  var newArray = [];
  // Add your code below this line
  for(var i = 0;i < this.length;i++)
  {
  	newArray.push(callback(this[i]));
  }
  // Add your code above this line
  return newArray;

};

var new_s = s.myMap(function(item){
  return item * 2;
});

console.log(new_s);

I wanted to try implementing it with forEach (The for loop makes sense for the most part…)

however something like this DOES NOT work:

// the global Array
var s = [23, 65, 98, 5];

Array.prototype.myMap = function(callback){
  var newArray = [];
  // Add your code below this line
  newArray.forEach(function(item,index,array){
  	newArray.push(callback(this[index]))
  })
  // Add your code above this line
  return newArray;

};

var new_s = s.myMap(function(item){
  return item * 2;
});

console.log(new_s);

Maybe im not understanding how forEach works? can you not manipulate the array itself?

2 Likes

You should use the forEach on the old array since the newArray is the one you are pushing values into. In your case the array s. Considering your solution would otherwise work, that’s probably a typo you didn’t recognize.

2 Likes

Doh! :smacks forehead: Can’t believe I didn’t even think of that, I got it worked (had to change callback(this[index])) to callback(element) in the loop of course.

That begs the question, when using the s.forEach how come I don’t need to say “this[index]” actually saying this[index] does not work. But shouldn’t this refer to s, which is the array? I checked what this was when doing s.forEach and “this” referred to the window object?

Why is that? I mean I am calling s.forEach So I would think even if this was getting pulled down to the forEach method it should still refer to s?

Or is it because forEach is a global Javascript method and this refers to it’s original array prototype or something?

That makes sense, sidenote though…when you do the push, whats the point of including index,arr in it as well? (in fact im confused why it even works)

Thanks for this.
Would not have figured this out on my own.

Dang… i knew how to get it to work but i didn’t knew how to catch the array. How did you know to refer at the Array as this?

1 Like

this is the thing to the left of the . (the object you are calling the method on). so for [1,2,3].map(), in map, this is [1,2,3]

5 Likes

i’ve passed it like this :
s.forEach((a)=>{return newArray.push(a*2);});

1 Like

How does the callback became a function in newArray.push(callback(item,index,arr)); where it was a parameter first on Array.prototype.myMap = function(callback) { ...?

@isemaj yeah, im also curios about that. it’s just a generic function parameter (change callback's to anything and it’ll pass)
but, i still don’t quite get it, we’ll wait for @camperextraordinaire to explain :grinning:

@isemaj and @mita

You were asking how does the callback become a function?
Refer to this part of the code to understand:

In the above section, the function called myMap is being given something called “an anonymous function”.
That’s the part that starts with “function(item) {”
The anonymous function is defined in the 3 lines here and passed into myMap .
So myMap’s parameter is itself a function which can be executed.

I know it is weird if you’ve never seen functions being passed as parameters before.

1 Like

@camperextraordinaire and @hbar1st thank you both for explaining this to me… now i get it :grinning::nerd_face:

1 Like

Hi Randle,
I have tried to configure maMap in this way:
Array.prototype.myMap = function(callback){
var newArray = ;
// Add your code below this line
for (let data of Object.keys(callback)) {
newArray.push(callback[data])
}
// Add your code above this line
console.log(newArray)
return newArray;
};

var new_s = s.myMap(s)

But I can only map with variable, not a function, may I know what is the difference?

Thanks Alot

Anson

I am using it but not sure if I completely understand,
let a = [1,2,3]
a.map(function(num){return console.log(num*2) })

I should get 2,4,6

map here is helping to map each number in a with the function
function mul(num) {
return console.log(num*2)
}
a.map(mul) get me the same result

Sth wrong in my code or I have some misunderstanding somewhere, but I cannot figure it out now at the moment. =[

Array.prototype.myMap = function(callback){
var newArray = [];
// Add your code below this line
// for (let i=0; i<this.length; i++) {
// newArray.push(callback(this[i]))
// }
for (let data of this) {
newArray.push(callback(data))
}
// Add your code above this line
return newArray;

};

after going through some discussion in the past of the forum, I seem to have better understand now. I will study further about the link you provided earlier =]

thanks a lot for your help =]