Use Array#reduce to implement a simple version of Array#map.
A function map
that applies a function to each item in an array and collects the results in a new Array.
var nums = [1,2,3,4,5]
// `map` is your exported function
var output = map(nums, function double(item) {
return item * 2
})
console.log(output) // => [2,4,6,8,10]
- input: an arbitrary Array of any type.
- operation: an arbitrary Function which can be applied to items in
input
.
- No need to implement the optional
thisArg
argument ofArray.prototype.map
, bonus points if you do!
- https://en.wikipedia.org/wiki/Reduce_(higher-order_function)
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce
function arrayMap(arr, fn, thisArg) {
//SOLUTION GOES HERE
}
module.exports = arrayMap;