complex json value update code example
Example 1: how to update value in nested json using id in javascript
function getObjects(obj, key, val, newVal) {
var newValue = newVal;
var objects = [];
for (var i in obj) {
if (!obj.hasOwnProperty(i)) continue;
if (typeof obj[i] == 'object') {
objects = objects.concat(getObjects(obj[i], key, val, newValue));
} else if (i == key && obj[key] == val) {
obj[key] = 'qwe';
}
}
return obj;
}
Example 2: how to update value in nested json using id in javascript
function getObjects(obj, key, val, newVal) {
for (var i in obj) {
if (!obj.hasOwnProperty(i)) continue;
if (i == key && obj[key] == val) {
obj[key] = newVal;
}
}
return obj
}