javascript - replace only some value from json to json -
here example:
i have jsona
{ "a":"1", "b":"2", "c":{"a":"1", "b":"2"} }
and jsonb
{ "b":"2new", "c":{"a":"1new"} }
i want update first jsona new value in jsonb and, @ end, have result:
{ "a":"1", "b":"2new", "c":{"a":"1new", "b":"2"} }
manually set every value, like:
jsona.b = jsonb.b; jsona.c.a = jsonb.c.a;
there way automatically without check every valule foreach?
i wrote this:
jsona = { "a":"1", "b":"2", "c":{"a":"1", "b":"2"} } jsonb = { "b":"2new", "c":{"a":"1new"} } (var j in jsona) { if(jsonb.hasownproperty(j)) { if (typeof jsona[j] === 'object') { (var in jsona[j]) { if(jsonb[j].hasownproperty(i)) { jsona[j][i] = jsonb[j][i]; } } } else { jsona[j] = jsonb[j]; } } }
check out here: http://jsfiddle.net/ytgqs/
Comments
Post a Comment