数组内的JSON Object数组在javascript中查找和替换

时间:2014-06-30 04:08:14

标签: javascript arrays json angularjs

我有一个像这样的JSON对象:

var myObject = [    
{
    "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"
         }
    ]
},
{
    "Name" : "app2",
    "id" : "2",
    "groups" : [
        { "id" : "test3", 
          "name" : "test group 4", 
          "desc" : "this is a test group"
         },
        { "id" : "test4",
          "name" : "test group 4",
          "desc" : "this is another test group"
         }
    ]
},
 {
    "Name" : "app3",
    "id" : "3",
    "groups" : [
        { "id" : "test5", 
          "name" : "test group 5", 
          "desc" : "this is a test group"
         },
        { "id" : "test6",
          "name" : "test group 6",
          "desc" : "this is another test group"
         }
    ]
}

];

我有新的价值"名称"具体" id"。 如何更换" name"特定的" id"在任何物体内?

如何计算所有对象中的组总数?

例如:将名称替换为"测试grp45"对于id =" test1"

这是小提琴 http://jsfiddle.net/qLTB7/21/

6 个答案:

答案 0 :(得分:14)

以下函数将搜索对象及其所有子对象/数组,并用新值替换该键。它将全球适用,因此在第一次更换后不会停止。取消注释注释行以使其成为那样。

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

工作jsfiddle:http://jsfiddle.net/qLTB7/28/

答案 1 :(得分:1)

试试这个

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

答案 2 :(得分:1)

这是使用Array.prototype.some的不同方法。它假定外部对象中的 Name 属性实际上应该是 name (注意大小写)。

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

some 的优点是,只要回调返回true,它就会停止。

答案 3 :(得分:0)

我认为这应该适合你: -

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

答案 4 :(得分:0)

也许是一个更简洁的解决方案

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

个人:如果这是我,在创建这些对象时,我会创建一个新的obj并按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"
            }
        ]
    }
}

然后你可以这样做:

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

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

答案 5 :(得分:0)

它应该是 if(object [“ id”] ==值),而不是 if(object [x] ==值) ,因此整个功能将如下所示:

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

如果离开对象[x]-函数还将用其他键值设置为“ test1”的对象替换名称(例如) {“ id”:“ xxx”,“名称”:“测试组1”,“ desc”:“ test1” }