3

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.

13
  • 4
    Hey, what have you tried? Commented Apr 1, 2020 at 9:22
  • 1
    There is no "JSON array", you have an array literal. Commented Apr 1, 2020 at 9:29
  • 1
    @kalaiyarasiM, Hope this is your final expected output codepen.io/Maniraj_Murugan/pen/PoqLBgK .. Look at console for your needed data.. Also your forked fiddle here jsfiddle.net/bjwytmz3 Commented Apr 1, 2020 at 9:57
  • 1
    @ManirajMuruganAs its needed for dynamic, i added variable name and its not working. Throwing error. tried to solve, but not working. Here is my Fiddle jsfiddle.net/bg7Lyvut/1 Commented Apr 2, 2020 at 4:16
  • 1
    @kalaiyarasiM, Also posted answer in another question, do accept it if it resolves your team issue.. Commented Apr 2, 2020 at 6:21

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Sorry, when i am adding variable name instead of calling it directly, its not working.
Sorry, not working. Dont know what I am missing. Please check my Fiddle. jsfiddle.net/eyvojfL2
0

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

Comments

-1

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)

3 Comments

I tried the same method, but its not working. Here is jsfiddle.net/pu5wev07
in your fiddle, @kalaiyarasiM since the main_data is an array of objects, you should first specify the index, then use the dot operator to select the object member, change main_data.client to main_data[0].client it will work.
@kalaiyarasiM–don't post your code elsewhere and link to it from a comment to an answer. Put it in the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.