0

my data structure has become a little messy since I have to pull separate data due to the API I'm using...

I have an array of objects called 'stocks' that looks like this:

[{id: "GOOGL", amount: 2},
{id: "IBM", amount: 10},
{id: "TSLA", amount: 9}]

and a separate array called 'currentPrice' that looks like this:

[1639, 125, 2213] 

I am zipping as suggested in this previous posted answer: How do I zip two arrays in JavaScript?

but it is pushing the element outside of the object, and just making it more complicated with nested arrays and objects..

Currently returning:

[[{id: "GOOGL", amount: 2}, 1639],
 [{id: "IBM", amount: 10}, 125],
 [{id: "TSLA", amount: 9}, 1639]]

Need:

[{id: "GOOGL", amount: 2, currentPrice: 1639},
 {id: "IBM", amount: 10, currentPrice: 125},
 {id: "TSLA", amount: 9, currentPrice: 1639}]

useEffect(() => {
    let c = stocks.map(function(e, i){
        return [e, currentPrice[i]]
    })
    console.log(c)
})

Edited, thank you!

butterup
  • 75
  • 4

0 Answers0