json - How to search all levels nested Javascript object -


i have javascript object represents nested json string this:

http://pastebin.com/vwzb1xra

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

working demo


Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -