Let's say I have a function and an array. I want to modify the array by applying the function to each entry in the array. The function does NOT modify the value directly; it returns a new value.
In pseudo-code,
for (entry in array) {
entry = function(entry);
}
There are a couple ways to do this that occurred to me:
for (var i = 0; i < arr.length; i++) {
arr[i] = fn(i);
}
Or, since I am using node.js and have underscore built in:
arr = _.map(arr, fn);
But this both seem a little clunky. The standard "for" block feels overly verbose, and the _.map function re-assigns the entire array so feels inefficient.
How would you do this?
Yes, I am aware I'm overthinking this :)