json - How to search all levels nested Javascript object -
i have javascript object represents nested json string this:
there many levels contain "name" element. example "svi" or "population" or "human development index".
i trying find way iterate on every level of object find name "svi" or "population" or "human development index". when match made want replace weight value this:
if (data[key].name == name) { data[key].weight = 55; }
you can recursively check each , every object, this
function rec(currentobject, values, replacement) { if (values.some(function(currentvalue) { return currentobject.name === currentvalue; })) { currentobject.weight = replacement; } (currentobject.children || []).foreach(function(currentitem) { rec(currentitem, values, replacement); }); } rec(data, ["svi", "population", "human development index"], 55); console.log(data);
Comments
Post a Comment