Is there any way to replace the existing object inside JSON array with new object based on key

Viewed 118

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.

3 Answers

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);

There may very well be a fancy way to get this done but one can always just find the matching item and replace it. eg.

   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"
                        }
                    }
                }
            ]
        }
    ];

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

    console.log("before:" + JSON.stringify(main_data));

    newItem = new_data[0]["client"][0];
    mainDataList = main_data[0]["client"];

    for (i = 0; i < mainDataList.length; i++) {
        if (mainDataList[i].name == newItem.name) {
            mainDataList[i] = newItem;
        }
    }
    console.log("after:" + JSON.stringify(main_data));

will output

before:[{"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"}}}]}]
after:[{"client":[{"name":"aaaa","count":"1","filter":{"type":{"name":"test3"}}},{"name":"bbbb","count":"1234","type":{"name":"updated_name"}}]},{"compute":[{"name":"cccc","count":"6","filter":{"type":{"name":"test"}}}]}]

Here's a simple way to do this, let's say your new data is at variable newData:

main_data.client.filter(item => item.name === newData.name).push(newData)
Related