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
- update property of object: how set javascript object values dynamically?
- but need update them all: how loop through or enumerate javascript object?
- but these objects inside array: loop through array in javascript
- but why have different mechanism loop objects , arrays?
for
cannot keys can usefor..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
Post a Comment