I have an array of objects as folllows
[
{\"width\":128.90663423245883,\"height\":160,\"X\":0,\"Y\":140},
{\"width\":277.0938568683375,\"height\":263,\"X
Here is an example using a slice to first return the array with the correct length and the a reduce to return the sum
const getSum = (objNum, arr) => {
const newArr = arr.slice(0, objNum - 1)
return newArr.reduce((current, next) => {
return current + next.width;
}, 0)
}
and in ES5
var getSum = (objNum, arr) => {
vat newArr = arr.slice(0, objNum - 1)
return newArr.reduce(function(current, next) {
return current + next.width;
}, 0)
}
and in 1 line
const getSum = (objNum, arr) => arr.slice(0, objNum - 1).reduce((current, next) => current + next.width, 0)