I am currently developing a JavaScript code that involves updating one JSON based on conditions from another JSON. Below is the code I have written.
const a = [{
"a": "Not Started"
}, {
"b": "Not Started"
}, {
"c": "Not Started"
}, {
"d": "Not Started"
}, {
"e": "Not Started"
}];
const b = [{
"Id": 1,
"Stage": "c"
}];
a.forEach((obj) => {
for (const [key, value] of Object.entries(a)) {
a.value = 'complete'
if (key == b[0].Stage)
break
}
});
console.log(a);
The goal here is to check the value of the Stage
key in the b
JSON variable and then iterate through the a
JSON variable. If there is a match between the keys, update the corresponding value in the a
variable to complete
, leaving everything else unchanged.
Based on the above code snippet, the expected output should be:
[{
"a": "complete"
}, {
"b": "complete"
}, {
"c": "complete"
}, {
"d": "Not Started"
}, {
"e": "Not Started"
}];
This is because the Stage
value in the b
JSON is set to c
.
If you have any suggestions on how to achieve this, please let me know.
Thank you!