[Solved] How to update Json object value dynamically in jquery?


jsonObj is an array thus you first have to iterate this array

jsonObj.forEach(o => {...});

o is now an object. You have to iterate the keys/values of it

for(let k in o)

k is a key in the object. You can now alter the value

o[k] = 'WhateverYouNeed'

var jsonObj = [{
       "key1": "value1",
       "key2": "value2", 
       "key3": "value3" 
     }, 
     {
       "key1": "value1", 
       "key2": "value2",
       "key3": "value3" 
}];

jsonObj.forEach(o => {
  for(let k in o)
    o[k] = 'ChangedValue'
});

console.log(jsonObj);

References:

As stated, your structure is an array of objects and not JSON: Javascript object Vs JSON

Now breaking you problem into parts, you need to

  1. Update property of an object: How to set a Javascript object values dynamically?
  2. But you need to update them all: How do I loop through or enumerate a JavaScript object?
  3. But these objects are inside an array: Loop through an array in JavaScript
  4. But why do I have different mechanism to loop for Objects and Arrays? for cannot get keys but we can use for..in over arrays. Right? No, you should not. Why is using “for…in” with array iteration a bad idea?

ReadMe links:

Please refer following link and check browser compatibility as the solution will not work in older browsers. There are other ways to loop which are highlighted in above link. Refer compatibility for them as well before using them.

5

solved How to update Json object value dynamically in jquery?