Home > Software engineering >  Find and Update in nested array of objects in javascript
Find and Update in nested array of objects in javascript

Time:04-12

Here is the array of array's

      let A = {
        "profileId": "12345",
        "data": [{
                "profileId": "12345",
                "campaignId": "39641",
                "portfolioData": {
                    "portfolioData": [{
                        "asin": "B08FSSHG17ZH",
                        "sku": ["130511"]
                    }, {
                        "asin": "B0OH8FHF74TB",
                        "sku": ["130512"]
                    }, {
                        "asin": "B0ZP8FHBWP89",
                        "sku": ["130513"]
                    }]
                }
            },
            {
                "profileId": "12345",
                "campaignId": "35551",
                "portfolioData": {
                    "portfolioData": [{
                        "asin": "B08FSSHG17ZH",
                        "sku": ["130511"]
                    }, {
                        "asin": "B0OH8FHF74TB",
                        "sku": ["130512"]
                    }, {
                        "asin": "B0ZP8FHBWP89",
                        "sku": ["130513"]
                    }]
                }
            }
    
        ]
    }

    let B = [{
        asin: 'B08FSSHG17ZH',
        sku: '130523221'
    },
    {
        asin: 'B0OH8FHF74TB',
        sku: '132122182'
    }
]

So i want to take array B and search the value with the help of asin in array A and update the sku in array. So the expected output i am looking for is:-

{
    "profileId": "12345",
    "data": [{
            "profileId": "12345",
            "campaignId": "39641",
            "portfolioData": {
                "portfolioData": [{
                    "asin": "B08FSSHG17ZH",
                    "sku": ["130511", "130523221"]
                }, {
                    "asin": "B0OH8FHF74TB",
                    "sku": ["130512", "132122182"]
                }, {
                    "asin": "B0ZP8FHBWP89",
                    "sku": ["130513"]
                }]
            }
        },
        {
            "profileId": "12345",
            "campaignId": "35551",
            "portfolioData": {
                "portfolioData": [{
                    "asin": "B08FSSHG17ZH",
                    "sku": ["130511", "130523221"]
                }, {
                    "asin": "B0OH8FHF74TB",
                    "sku": ["130512", "132122182"]
                }, {
                    "asin": "B0ZP8FHBWP89",
                    "sku": ["130513"]
                }]
            }
        }

    ]
}

I am trying to push new value in the sku array which is inside of array of array. So i am trying to update with the function like:-

 function update(columns, responseData) {
          const helper = new Map();
          for (const item of responseData) helper.set(item.asin, item);

          for (const item of columns) {
            const s = helper.get(item.asin);
            item.sku.push(item.sku);
          }
        }

update(A, B);

I know this function wont execute as it is array of array of objects. Where as the function i have written is for array of objects.

CodePudding user response:

This is not optimised,
and also I would suggest to remove one portfolioData,
and let the portfolioData be an object with keys equal to your current asin,
but anyway - here's the working solution:

update = (mainArray, values) => {
  return {...mainArray, data: mainArray.data.map(item => {
    data = {...item.portfolioData.portfolioData}
    return {
      ...item,
      portfolioData: {
        portfolioData: values.reduce((prev, curr) => {
          elWithAsin = data.find(el => el.asin === curr.asin);
          if (elWithAsin) {
            elWithAsin.sku.push(curr.sku);
            tempData = data.filter(el => el.asin !== curr.asin)
            return tempData.concat(elWithAsin)
          } else {
            return data
          }
        }, data)
      }
    }
  })}
}
  • Related