JSON Object array inside array find and replace in javascript

后端 未结 6 1364
长发绾君心
长发绾君心 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:48

    Maybe a more succinct sol'n

    function changeName(objArray, objId, newName) {
        objArray.forEach(function(obj) {
            if (obj.id === objId) obj.Name = newName;
        });
    }
    

    Personally: if this were me, when creating these objects, I would create a new obj and key them by id.

     var myApps = {};
     myObject.forEach(function(o) {
         myApps[o.id] = o;
     });
    
    =>
    {
        "1": {
            "Name": "app1",
            "id": "1",
            "groups": [
                {
                    "id": "test1",
                    "name": "test group 1",
                    "desc": "this is a test group"
                },
                {
                    "id": "test2",
                    "name": "test group 2",
                    "desc": "this is another test group"
                }
            ]
        }
    }
    

    And then you could just do:

    myApps['someId'].name = 'This is my new Name'
    

    Check it out here: http://jsfiddle.net/qLTB7/40/

    0 讨论(0)
  • 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
    });
    });
    
    0 讨论(0)
  • 2020-12-18 01:54

    it should be if (object["id"] == value) instead of if (object[x] == value) in 7th line of PitaJ answer, so whole function will look like:

    function findAndReplace(object, value, replacevalue) {
      for (var x in object) {
        if (object.hasOwnProperty(x)) {
          if (typeof object[x] == 'object') {
            findAndReplace(object[x], value, replacevalue);
          }
          if (object["id"] == value) { 
            object["name"] = replacevalue;
            // break; // uncomment to stop after first replacement
          }
        }
      }
    }

    if you leave object[x] - function will replace name also for objects with other keys values set to "test1", for example {"id": "xxx", "name": "test group 1", "desc": "test1"}

    0 讨论(0)
  • 2020-12-18 02:04

    Try this

    function findAndReplace(object,keyvalue, name) {
        object.map(function (a) {
            if (a.groups[0].id == keyvalue) {
                a.groups[0].name = name
            }
        })
    }
    
    findAndReplace(myObject,"test1" ,"test grp45");
    
    0 讨论(0)
  • 2020-12-18 02:05

    The following function will search through an object and all of its child objects/arrays, and replace the key with the new value. It will apply globally, so it won't stop after the first replacement. Uncomment the commented line to make it that way.

    function findAndReplace(object, value, replacevalue) {
      for (var x in object) {
        if (object.hasOwnProperty(x)) {
          if (typeof object[x] == 'object') {
            findAndReplace(object[x], value, replacevalue);
          }
          if (object[x] == value) { 
            object["name"] = replacevalue;
            // break; // uncomment to stop after first replacement
          }
        }
      }
    }
    

    Working jsfiddle: http://jsfiddle.net/qLTB7/28/

    0 讨论(0)
  • 2020-12-18 02:15

    Here's a different approach using Array.prototype.some. It assumes that the Name property in the outer objects should be actually be name (note capitalisation).

    function updateNameById(obj, id, value) {
      Object.keys(obj).some(function(key) {
        if (obj[key].id == id) {
          obj[key].name = value;
          return true;  // Stops looping
        }
          // Recurse over lower objects
          else if (obj[key].groups) {
          return updateNameById(obj[key].groups, id, value);
        }
      })
    }
    

    The advantage of some is that it stops as soon as the callback returns true.

    0 讨论(0)
提交回复
热议问题