I want to sum each price with pervious value as a new array this is my object and my final array I except with my reducer I write
data=[{price: 2, volume: 123}
{price: 3, volume: 123}
{price: 4, volume: 254}
{price: 1, volume: 444}
{price: 5, volume: 555}]
finaldata=[{price: 2, volume: 555}
1: {price: 5, volume: 777}
2: {price: 9, volume: 5000}
3: {price: 10, volume: 8000}
4: {price: 15, volume: 4000}]
const test = buy.map((items, index) =>
items.reduce((prev, curr, currIndex) => {
return index >= currIndex ? { volume: (prev?.volume || 0) curr?.volume } : 0;
}, 0)
);
cons
CodePudding user response:
It's better to use reduce
for sum each price with pervious value as a new array, not map
const data=[{price: 2, volume: 123},
{price: 3, volume: 123},
{price: 4, volume: 254},
{price: 1, volume: 444},
{price: 5, volume: 555}]
const result =
data.reduce((acc, curr, currIndex) => {
const lastItem = acc[acc.length - 1];
acc.push({ ...curr, price: lastItem ? lastItem.price curr.price : curr.price })
return acc;
}, [])
console.log(result)
CodePudding user response:
i would use like this, first i concat all data from first data with finaldata then i group it, if price is same then it will increase the volume
const data = [
{price: 2, volume: 123},
{price: 3, volume: 123},
{price: 4, volume: 254},
{price: 1, volume: 444},
{price: 5, volume: 555}
]
const finaldata = [
{price: 2, volume: 555},
{price: 5, volume: 777},
{price: 9, volume: 5000},
{price: 10, volume: 8000},
{price: 15, volume: 4000}
]
const result = [...data].concat(finaldata).reduce((previous, {price, volume}) => {
const find = previous.find(item => item.price === price);
if (find) {
find.volume = find.volume volume
} else {
previous.push({price, volume})
}
return previous
}, [])
console.log(result)
CodePudding user response:
You can also achieve this with a single line of code by using Array.map()
method.
Live Demo :
const data=[{price: 2, volume: 123},
{price: 3, volume: 123},
{price: 4, volume: 254},
{price: 1, volume: 444},
{price: 5, volume: 555}];
const finalData = data.map((obj, index) => {
if (index < data.length - 1) data[index 1].price = data[index 1].price obj.price;
return obj;
});
console.log(finalData);
CodePudding user response:
I publish my solution here because I think it is easier to understand than the other solutions.
const data = [
{ price: 2, volume: 123 },
{ price: 3, volume: 123 },
{ price: 4, volume: 254 },
{ price: 1, volume: 444 },
{ price: 5, volume: 555 }
]
let priceAccum = 0
const res = data.map((item) => {
priceAccum = item.price
return { ...item, price: priceAccum }
})
console.table(res)
We accumulate price in a variable and replace the item's price with the accumulated value. The code is not a pure function. Using reduce purifies it, but it sacrifices simplicity.
CodePudding user response:
There are so many solutions for all tastes in this post. My approach is a .reduce
with a complex accamulator in a non-mutable way:
const inputData = [{price:2,volume:123},{price:3,volume:123},{price:4,volume:254},{price:1,volume:444},{price:5,volume:555}];
const runningTotals = inputData
.reduce((acc, item) => ({
total: acc.total item.price,
data: [...acc.data, { ...item, price: acc.total item.price }],
}), { total:0, data:[] })
.data;
console.log(runningTotals);
.as-console-wrapper { max-height: 100% !important; top: 0 }