alexa

React looping and creating new array with nested objects in javascript ?

React looping and creating new array with nested objects in javascript ?

You can use reduce to group positions by userid.

 const positions =  [{
      "name": "AAPL",
      "ownerTotals": {uid: "xxx", totaledAmount: 140 }, {uid: "yyy", totaledAmount: 10}
    },
    {
      "name": "TSLA",
      "ownerTotals": {uid: "xxx", totaledAmount: 11 }, {uid: "yyy", totaledAmount: 2}
    }]

const posByUid = positions.reduce((acc, current) => {
 const name = current.name
 current.positions.forEach(position => {
   if (!acc[position.uid]) {
    acc[position.uid] = []
   }

   acc[position.uid] = {name, totaledAmount: position.totaledAmount}
 })
 return acc
}, {})

224 0
7

Write a Comments


* Be the first to Make Comment

GoodFirms Badge
GoodFirms Badge

Fix Your Meeting With Our SEO Consultants in India To Grow Your Business Online

Facebook
Twitter
LinkedIn
Instagram
Whatsapp
Call Now
Quick Inquiry