Home > Blockchain >  sum each price with previous value as a new value in react js
sum each price with previous value as a new value in react js

Time:09-05

I have an array of object and I want sum each price value with last price as a new value this is my data with final data I

data=[{price: 2, volume: 123}
{price: 3, volume: 123}
{price: 4, volume: 254}
{price: 1, volume: 444}
{price: 5, volume: 555}]

finalfata=[{price: 2, volume: 555}
1: {price: 5, volume: 777}
2: {price: 9, volume: 5000}
3: {price: 10, volume: 8000}
4: {price: 15, volume: 4000}]

except

CodePudding user response:

data.map((element, index)=>{element.price  = (index != 0) ? data[index-1].price : 0})

CodePudding user response:

let prevPrice = 0
let prevVolume = 0
data.forEach(d => { prevPrice  = d.price; prevVolume  = d.volume; 
    d.price = prevPrice; 
    d.volume = prevVolume;
    return d;
})
  • Related