Is there a simple magical function combination from underscore (lo-dash) that can achieve the following algorithm:
var arr = [1, 4, 9, 16, 25];
var result = [];
for(var i = 1, len = arr.length; i < len; ++i) {
var current = arr[i];
var previous = arr[i-1];
result[i-1] = current - previous;
}
// result == [3, 5, 7, 9]
with a more concise syntax:
var arr = [1, 4, 9, 16, 25];
var result = _.???(arr, function(current, previous) {
return current - previous;
}
Can this be done with existing methods?
Additional Information:
- I prefer not to write my own function and would rather utilize combinations of existing functions.
- The arrays in question actually contain objects, not numbers, and the resulting output may or may not be numeric.