Created
December 11, 2017 20:29
-
-
Save amandarfernandes/90e8d8bd8a815c8d319377ff06584453 to your computer and use it in GitHub Desktop.
JSArray Map
This file contains hidden or 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
/* | |
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