< hr >
08:27:41 on September 10, 2018
I ran away happily after work last Friday. I came back today to see such a set of data
.
data {
0: arr[0] + arr[1],
1: arr[0] + arr[1] + arr[2],
2: arr[0] + arr[1] + arr[2] + arr[3]
}
is equivalent to
data {
0: arr[0] + arr[1],
1: data[0] + arr[2],
2: data[1] + arr[3]
}
if the data organization is an array [1, code, 2, 3, 4, 5]
arr = [1,2,3,4,5],sum = 0;
arr.map(v=>sum+=v)
,.slice(1)
< hr >
original answer
you don't quite understand this description, step size is 2? Is that so?
1 2 3 4 i
2 4 6 8 i*2
1 3 5 7 i*2-1
let arr = [
{ width: 155 }, { width: 180 }, { width: 180 }, { width: 240 },
{ width: 130 }, { width: 280 }, { width: 100 }, { width: 50 }
]
let result = arr.reduce((pre, cur, index) => {
let sum = Object.keys(pre).length > 0 ? pre[index - 1] + cur.width : 0 + cur.width;
pre[index] = sum;
return pre;
}, {})
console.log('result', result)
let data = arr.reduce((sum, cur, index) => {
sum[index] = index === 0 ? cur.width : sum[index - 1] + cur.width
return sum
}, {})