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?
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.
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)
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
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.
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?
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 =]