Ad

Is There Any Way To Replace The Existing Object Inside JSON Array With New Object Based On Key

- 1 answer

I am having a dynamic JSON array in below format,

let main_data = [
   {
      "client":[
         {
            "name":"aaaa",
            "count":"1",
            "filter":{
               "type":{
                  "name":"test3"
               }
            }
         },
         {
            "name":"bbbb",
            "count":"9",
            "filter":{
               "type":{
                  "name":"test2"
               }
            }
         }
      ]
   },
   {
      "compute":[
         {
            "name":"cccc",
            "count":"6",
            "filter":{
               "type":{
                  "name":"test"
               }
            }
         }
      ]
   }
]

Here key "name" is unique. When updating a form, I will get an json array like below,

let new_data = [
   {
      "client":[
         {
            "name":"bbbb",
            "count":"1234",
               "type":{
                  "name":"updated_name"
               }
            }
         }
      ]
   }
]

I need to check the "name" in the json array in "main_data" and remove the existing one and update with the new "updated_data" into the "main_data". (no Jquery please)

Expected output,

let main_data = [
   {
      "client":[
         {
            "name":"aaaa",
            "count":"1",
            "filter":{
               "type":{
                  "name":"test3"
               }
            }
         },
         {
            "name":"bbbb",
            "count":"123",
            "filter":{
               "type":{
                  "name":"updated_name"
               }
            }
         }
      ]
   },
   {
      "compute":[
         {
            "name":"cccc",
            "count":"6",
            "filter":{
               "type":{
                  "name":"test"
               }
            }
         }
      ]
   }
]

Is there any way to achive this. Any help would be much appreciated. Thanks in advance.

Ad

Answer

Try this

let main_data = [{
    client: [{
            name: "aaaa",
            count: "1",
            filter: {
                type: {
                    name: "test3"
                }
            }
        },
        {
            name: "bbbb",
            count: "9",
            filter: {
                type: {
                    name: "test2"
                }
            }
        }
    ]
},
{
    compute: [{
        name: "cccc",
        count: "6",
        filter: {
            type: {
                name: "test"
            }
        }
    }]
}
];
let new_data = [{
client: [{
    name: "bbbb",
    count: "1234",
    filter: {
        type: {
            name: "updated_name"
        }
    }
}]
}];

const res = main_data.map((item, index) => {
if (item.client) {
    const clients = item.client.map(client => {
        if (client.name === new_data[0].client[0].name) {
            client = new_data[0].client[0];
        }
        return client;
    });
    return {
        client: clients
    };
}
return item;
});

console.log(res);

Ad
source: stackoverflow.com
Ad