Skip to content

Instantly share code, notes, and snippets.

@amandarfernandes
Created December 11, 2017 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amandarfernandes/90e8d8bd8a815c8d319377ff06584453 to your computer and use it in GitHub Desktop.
Save amandarfernandes/90e8d8bd8a815c8d319377ff06584453 to your computer and use it in GitHub Desktop.
JSArray Map
/*
Write a function called doubleValues which accepts an array and returns a new array with all the values in the array passed to the function doubled
Examples:
doubleValues([1,2,3]) // [2,4,6]
doubleValues([1,-2,-3]) // [2,-4,-6]
*/
function doubleValues(arr){
return arr.map(function(value,index,array){
return value*2;
});
}
/*
Write a function called valTimesIndex which accepts an array and returns a new array with each value multiplied by the index it is currently at in the array.
Examples:
valTimesIndex([1,2,3]) // [0,2,6]
valTimesIndex([1,-2,-3]) // [0,-2,-6]
*/
function valTimesIndex(arr){
return arr.map(function(value,index,array){
return value*index;
});
}
/*
Write a function called extractKey which accepts an array of objects and some key and returns a new array with the value of that key in each object.
Examples:
extractKey([{name: 'Elie'}, {name: 'Tim'}, {name: 'Matt'}, {name: 'Colt'}], 'name') // ['Elie', 'Tim', 'Matt', 'Colt']
*/
function extractKey(arr, key){
return arr.map(function(value,index,array){
return value[key];
});
}
/*
Write a function called extractFullName which accepts an array of objects and returns a new array with the value of the key with a name of "first" and the value of a key with the name of "last" in each object, concatenated together with a space.
Examples:
extractFullName([{first: 'Elie', last:"Schoppik"}, {first: 'Tim', last:"Garcia"}, {first: 'Matt', last:"Lane"}, {first: 'Colt', last:"Steele"}]) // ['Elie Schoppik', 'Tim Garcia', 'Matt Lane', 'Colt Steele']
*/
function extractFullName(arr){
return arr.map(function(value,index,array){
return value.first+" "+value.last;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment