Skip to content

Instantly share code, notes, and snippets.

@shahbazsyed
Created April 25, 2017 08:57
Show Gist options
  • Save shahbazsyed/5708a8f9e478b2e92aa1f80e59338a23 to your computer and use it in GitHub Desktop.
Save shahbazsyed/5708a8f9e478b2e92aa1f80e59338a23 to your computer and use it in GitHub Desktop.
Functional programming in Js - Map
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