admin管理员组文章数量:1415109
I'm writing a js code in which I need to update 1 JSON based on the condition from another JSON. Here are my code.
var a = [{
"a": "Not Started"
}, {
"b": "Not Started"
}, {
"c": "Not Started"
}, {
"d": "Not Started"
}, {
"e": "Not Started"
}];
var b = [{
"Id": 1,
"Stage": "c"
}];
a.forEach((obj) => {
for (const [key, value] of Object.entries(a)) {
a.value = 'plete'
if (key == b[0].Stage)
break
}
});
console.log(a);
I'm writing a js code in which I need to update 1 JSON based on the condition from another JSON. Here are my code.
var a = [{
"a": "Not Started"
}, {
"b": "Not Started"
}, {
"c": "Not Started"
}, {
"d": "Not Started"
}, {
"e": "Not Started"
}];
var b = [{
"Id": 1,
"Stage": "c"
}];
a.forEach((obj) => {
for (const [key, value] of Object.entries(a)) {
a.value = 'plete'
if (key == b[0].Stage)
break
}
});
console.log(a);
Here is what I'm trying to do. Check what is the Stage
value in b
JSON variable. then moving to my a
JSON variable and looping over it. If Key
matches, the Stage
value, till the particular key, I want to update the value in a
variable to plete
, rest, just return as it is.
From the above snippet, the expected output is.
[{
"a": "plete"
}, {
"b": "plete"
}, {
"c": "plete"
}, {
"d": "Not Started"
}, {
"e": "Not Started"
}];
Since the Stage
value in b
stage is c
.
Please let me know how I can achieve this.
Thanks
Share Improve this question asked Apr 29, 2021 at 18:46 user3872094user3872094 3,3619 gold badges38 silver badges78 bronze badges 1- 1 1. a is an array not an object, so there isn't any entries for a. 2. same goes for a.value (even if referencing the the obj it should be obj[key] not obj.value. 3. an array.forEach does not usually modifies the array, there is an array.map() for that. – ISAE Commented Apr 29, 2021 at 18:58
3 Answers
Reset to default 3if the keys of objects are alphabetical characters (assuming the example you have provided isn't a generalisation), you use take advantage of the ordering of character codes:
const a = [{
"a": "Not Started"
}, {
"b": "Not Started"
}, {
"c": "Not Started"
}, {
"d": "Not Started"
}, {
"e": "Not Started"
}];
const b = [{
"Id": 1,
"Stage": "c"
}];
for (obj of a) {
const key = Object.keys(obj)[0]
if (key.charCodeAt(0) <= b[0]["Stage"].charCodeAt(0)) {
obj[key] = "plete"
}
}
console.log(a)
if the alphabetical order isn't the case, you can follow the solution above
var a = [{
"a": "Not Started"
}, {
"b": "Not Started"
}, {
"c": "Not Started"
}, {
"d": "Not Started"
}, {
"e": "Not Started"
}];
var b = [{
"Id": 1,
"Stage": "c"
}];
var idx = a.findIndex(x => Object.keys(x)[0] === b[0].Stage);
for (var i = 0; i <= idx; i++){
const key = Object.keys(a[i])[0];
a[i][key] = "plete";
}
console.log(a);
More generic solution, in case where b
can contain more than 1 element:
var a = [{
"a": "Not Started"
}, {
"b": "Not Started"
}, {
"c": "Not Started"
}, {
"d": "Not Started"
}, {
"e": "Not Started"
}];
var b = [{
"Id": 1,
"Stage": "b"
}, {
"Id": 2,
"Stage": "c"
}];
const currStage = b.sort((a,b) => b["Id"] - a["Id"])[0]["Stage"]
a = a.map(e => {
const [[letter, state]] = Object.entries(e)
const inStage = letter.charCodeAt(0) <= currStage.charCodeAt(0)
const currState = inStage ? "pleted" : state
return Object.fromEntries([[letter, currState]])
})
console.log(a)
本文标签: javascriptUnable to update a json value in a loopStack Overflow
版权声明:本文标题:javascript - Unable to update a json value in a loop - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745215875a2648162.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论