how to update value in nested json using id in javascript

Solutions on MaxInterview for how to update value in nested json using id in javascript by the best coders in the world

we are a community of more than 2 million smartest coders
registration for
employee referral programs
are now open
get referred to google, amazon, flipkart and more
register now
  
showing results for - "how to update value in nested json using id in javascript"
Sherine
06 Mar 2019
1function getObjects(obj, key, val, newVal) {
2  for (var i in obj) {
3      if (!obj.hasOwnProperty(i)) continue;
4      if (i == key && obj[key] == val) {
5          obj[key] = newVal;
6      }
7  }
8  return obj
9}
10
Sara
24 Nov 2019
1function getObjects(obj, key, val, newVal) {
2    var newValue = newVal;
3    var objects = [];
4    for (var i in obj) {
5        if (!obj.hasOwnProperty(i)) continue;
6        if (typeof obj[i] == 'object') {
7            objects = objects.concat(getObjects(obj[i], key, val, newValue));
8        } else if (i == key && obj[key] == val) {
9            obj[key] = 'qwe';
10        }
11    }
12    return obj;
13}
14