i have an incoming array:
[{step: 0, count: 1}, {step: 1, count: 5}, {step: 5, count: 5}]
so i need to transform incoming array in another array
[0, 1, 2, 3, 4, 5, 10, 15, 20, 25, 30]
I have tried to go this way:
const convertRangeData = (rangeData) =>
{
const convertedRangeData =
rangeData.reduce( (acc, item) =>
{
const { step, count } = item;
const prev = acc[acc.length - 1];
return [...acc, ...[...Array(count)].fill(step).map((i, idx) => i * (idx + 1) + prev)];
},[0] )
return convertedRangeData;
}
but I've got
[0, 0, 1, 2, 3, 4, 5, 10, 15, 20, 25, 30]
question from:
https://stackoverflow.com/questions/65849430/building-an-array-of-sequential-numbers 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…