Created
April 25, 2017 08:57
-
-
Save shahbazsyed/5708a8f9e478b2e92aa1f80e59338a23 to your computer and use it in GitHub Desktop.
Functional programming in Js - Map
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Array.prototype.map = function(projectionFunction) { | |
var results = []; | |
this.forEach(function(itemInArray) { | |
/* | |
projectionFunction is applied to each element of the array, which returns the modified value of that element | |
*/ | |
results.push(projectionFunction(itemInArray)); | |
}); | |
return results; | |
}; | |
// Example | |
function() { | |
var popularMovies = [ | |
{ | |
"id": 1, | |
"title": "Inception", | |
"actors": ["Leo","Tom","Joseph"] | |
}, | |
{ | |
"id": 2, | |
"title": "Jason Bourne", | |
"actors": ["Matt","Alicia"] | |
}, | |
{ | |
"id": 3, | |
"title": "Interstellar", | |
"actors": ["Matthew","Matt","Anne"] | |
}, | |
{ | |
"id": 4, | |
"title": "Fast & Furious", | |
"actors": ["Vin","Paul","Dwayne"] | |
}, | |
]; | |
return popularMovies.map(function(movie) { return { id: movie.id, title: movie.title }; }); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment