admin管理员组文章数量:1426594
var jsonObj = [
{
"key1": "value1",
"key2": "value2",
"key3": "value3"
},
{
"key1": "value1",
"key2": "value2",
"key3": "value3"
}
];
I want to update all the values of all the objects. Here the keys and values are dynamic not key1 and value1. Could you help to figure it out the requirement.
I want to make each value of each object to "changedvalue". So the final result after updating the jsonObj is
[
{
"key1": "changedvalue",
"key2": "changedvalue",
"key3": "changedvalue"
},
{
"key1": "changedvalue",
"key2": "changedvalue",
"key3": "changedvalue"
}
];
var jsonObj = [
{
"key1": "value1",
"key2": "value2",
"key3": "value3"
},
{
"key1": "value1",
"key2": "value2",
"key3": "value3"
}
];
I want to update all the values of all the objects. Here the keys and values are dynamic not key1 and value1. Could you help to figure it out the requirement.
I want to make each value of each object to "changedvalue". So the final result after updating the jsonObj is
[
{
"key1": "changedvalue",
"key2": "changedvalue",
"key3": "changedvalue"
},
{
"key1": "changedvalue",
"key2": "changedvalue",
"key3": "changedvalue"
}
];
Share
Improve this question
edited Jul 26, 2017 at 6:12
Phil
165k25 gold badges262 silver badges267 bronze badges
asked Jul 26, 2017 at 5:56
Sandeep sandySandeep sandy
3971 gold badge7 silver badges14 bronze badges
3
- 2 So how exactly do you want the result to look? What is it you want to change? – Phil Commented Jul 26, 2017 at 6:00
- 2 I'm voting to close this question as off-topic because question does not show any sign of effort. The problem statement is inplete and unclear. – Rajesh Commented Jul 26, 2017 at 6:03
- Thanks @weedoze for providing a solution, it's really appreciable for encouraging dumbs like me. Ya it's my wrong for missing of result what I need, that doesn't make any sense because I want to modify the value then what's the deal about what is the result. I will maintain it for sure as seniors suggested. – Sandeep sandy Commented Jul 26, 2017 at 6:30
4 Answers
Reset to default 5jsonObj
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
- Update property of an object: How to set a Javascript object values dynamically?
- But you need to update them all: How do I loop through or enumerate a JavaScript object?
- But these objects are inside an array: Loop through an array in JavaScript
- But why do I have different mechanism to loop for Objects and Arrays?
for
cannot get keys but we can usefor..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 patibility as the solution will not work in older browsers. There are other ways to loop which are highlighted in above link. Refer patibility for them as well before using them.
- Arrow functions
- Array.forEach
Here you go with a solution https://jsfiddle/Lha8xk34/
var jsonObj = [{
"key1": "value1",
"key2": "value2",
"key3": "value3"
}, {
"key1": "value1",
"key2": "value2",
"key3": "value3"
}];
$.each(jsonObj, function(i) {
$.each(jsonObj[i], function(key) {
jsonObj[i][key] = "changed" + key;
});
});
console.log(jsonObj);
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
You might want to use a mixture of forEach
and Object.keys
const jsonObj = [
{
"key1": "value1",
"key2": "value2",
"key3": "value3"
},
{
"key1": "value1",
"key2": "value2",
"key3": "value3"
}
];
jsonObj.forEach( obj => {
Object.keys( obj ).forEach( key => {
const value = obj[ key ];
console.log( key, value );
} );
} );
Try pinch
in your Case :
var jsonObj = [
{
"key1": "value1",
"key2": "value2",
"key3": "value3"
},
{
"key1": "value1",
"key2": "value2",
"key3": "value3"
}
];
pinch(data, "/key1/", function(path, key, value) {
return (value === "value1") ? "updatedValue" : value;
});
本文标签: javascriptHow to update Json object value dynamically in jqueryStack Overflow
版权声明:本文标题:javascript - How to update Json object value dynamically in jquery? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745483730a2660289.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论