javascript - How to update Json object value dynamically in jquery? -


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

i want update values of objects. here keys , values dynamic not key1 , value1. figure out requirement.

i want make each value of each object "changedvalue". final result after updating jsonobj

[      {        "key1": "changedvalue",        "key2": "changedvalue",         "key3​": "changedvalue"       },       {        "key1": "changedvalue",         "key2": "changedvalue",        "key3​": "changedvalue"       } ]; 

jsonobj array first have iterate array

jsonobj.foreach(o => {...}); 

o object. have iterate keys/values of it

for(let k in o) 

k key in object. can alter 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, structure array of objects , not json: javascript object vs json

now breaking problem parts, need to

  1. update property of object: how set javascript object values dynamically?
  2. but need update them all: how loop through or enumerate javascript object?
  3. but these objects inside array: loop through array in javascript
  4. but why have different mechanism loop objects , arrays? for cannot keys can use for..in on arrays. right? no, should not. why using "for...in" array iteration bad idea?

readme links:

please refer following link , check browser compatibility solution not work in older browsers. there other ways loop highlighted in above link. refer compatibility them before using them.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -