JSFiddle - React, Tailwind, and code Playground
by Yuriy Petrichenko
JavaScript
const a = [{key: 1}, {key: 3},{key: 2},{key: 2},{key: 5},{key: 7} ];
const range = [3, 5];
let rangeToSplit = [];
let shouldCollect = false;
const s = a.reduce((acum, currentVal, index) => {
if(currentVal.key === range[0]) {
shouldCollect = true;
}
if(shouldCollect) {
if(currentVal.key !== range[1]) {
acum.total = acum.total + currentVal.key;
}else {
acum.total = acum.total + currentVal.key;
shouldCollect = false;
}
}
return acum;
}, {total: 0})
console.log(s);