javascript - Unable to update a json value in a loop - Stack Overflow

I'm writing a js code in which I need to update 1 JSON based on the condition from another JSON. H

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
Add a ment  | 

3 Answers 3

Reset to default 3

if 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)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745215876a4617016.html

相关推荐

  • javascript - Unable to update a json value in a loop - Stack Overflow

    I'm writing a js code in which I need to update 1 JSON based on the condition from another JSON. H

    7小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信