JSON object array inside array find and replace in javascript

I have one JSON object:

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

I have a new value available for "name" for a specific "id". How to replace the "name" of a specific "id" inside any object?

And how to calculate the total number of groups among all objects?

for example: replace the name with "test grp45" for id = "test1"

Here is the fiddle http://jsfiddle.net/qLTB7/21/

+6
source share
5 answers

The following function searches for the object and all its child objects / arrays and replaces the key with a new value. It will be applied worldwide, so it will not stop after the first replacement. Uncomment the commented line to do it this 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 } } } } 

Jsfiddle work: http://jsfiddle.net/qLTB7/28/

+11
source

Here, a different approach is used using Array.prototype.some . It assumes that the Name property in external objects should actually be a name (capital letter).

 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.

+1
source

try it

 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
source

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
source

Perhaps a more concise solution

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

Personally : if it were me, creating these objects, I would create a new obj and run 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 can just do:

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

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

0
source

Source: https://habr.com/ru/post/971518/


All Articles