JSON Object array inside array find and replace in javascript

后端 未结 6 1369
长发绾君心
长发绾君心 2020-12-18 01:21

I have one JSON Object like this :

var myObject = [    
{
    \"Name\" : \"app1\",
    \"id\" : \"1\",
    \"groups\" : [
        { \"id\" : \"test1\", 
             


        
6条回答
  •  一向
    一向 (楼主)
    2020-12-18 01:52

    I think this should work for you:-

    var id = 'test1';
    var newname = 'test grp45';
    var numberOfGruops = 0;
    myObject.forEach(function(app){
    numberOfGruops += app.groups.length;    //Count all groups in this app
    app.groups.forEach(function(group){
        if(group.id===id)
            group.name = newname;   // replace the name
    });
    });
    

提交回复
热议问题