替换嵌套对象的值如果键存在。

huangapple go评论66阅读模式
英文:

replace the value of a nested object if key exists

问题

我有一个包含嵌套对象的数组。我想要做的是循环遍历数组中的对象,并检查对象中是否存在某个键,如果存在,我想要替换该对象的值并跳出循环。如果该键在任何对象中都不存在,我想要在数组中添加一个新对象并打印最终的数组。我尝试的以下代码不起作用。我哪里出错了,如何修复它?谢谢。

x = [{
  "s": "23",
  "t": "41"
}, {
  "e": "29",
  "t": "87"
}]

for (var i = 0; i < x.length; i++) {
  if ("d" in x[i]) {
    x[i]["t"] = "21";
    console.log(x);
    break;
  } else if (i === x.length - 1) {
    x.push({
      "d": "22",
      "t": "77"
    });
    console.log(x);
  }
}

希望这有所帮助。

英文:

I have an array with nested objects. What I want to achieve is to loop through the arrays objects and check if a key exists inside the object and if it does I want to replace the value of that object and break out of the loop. If the key doesn't exist inside any of the objects I want to append a new object into the array and print the final array. What I tried below is not working. Where did I go wrong and how can I fix it? Thanks in advance.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

x = [{
  &quot;s&quot;: &quot;23&quot;,
  &quot;t&quot;: &quot;41&quot;
}, {
  &quot;e&quot;: &quot;29&quot;,
  &quot;t&quot;: &quot;87&quot;
}]

for (var i = 0; i &lt; x.length; i++) {
  if (&quot;d&quot; in x[i]) {
    x[i][&quot;t&quot;] = &quot;21&quot;
    console.log(x)
    break
  } else if (i === x.length - 1) {
    x.push({
      &quot;d&quot;: &quot;22&quot;,
      &quot;t&quot;: &quot;77&quot;
    })
    console.log(x)

    //loop finised
  }
}

<!-- end snippet -->

答案1

得分: 2

The reason your code isn't working because it enters an infinite loop. You're mutating the array directly (by adding/pushing a new element) while looping through it. So the length keeps getting longer & longer resulting in an infinite loop.

Just change your code to the following:

x = [{
  "s": "23",
  "t": "41"
}, {
  "e": "29",
  "t": "87"
}]

let isKeyExists = false;

for (var i = 0; i < x.length; i++) {
  if ("d" in x[i]) {
    x[i]["t"] = "21";
    console.log(x);
    isKeyExists = true;
    break;
  }  
}

if (!isKeyExists) {
  x.push({
      "v": "22",
      "t": "77"
  });
}

console.log(x);

In the code above, you just add the object after you have exited the loop.

英文:

The reason your code isn't working because it enters an infinite loop. You're mutating the array directly(by adding/pushing a new element) while looping through it. So the length keeps getting longer & longer resulting in infinite loop.

Just change your code to the following:

x = [{
  &quot;s&quot;: &quot;23&quot;,
  &quot;t&quot;: &quot;41&quot;
}, {
  &quot;e&quot;: &quot;29&quot;,
  &quot;t&quot;: &quot;87&quot;
}]

let isKeyExists = false;

for (var i = 0; i &lt; x.length; i++) {
  if (&quot;d&quot; in x[i]) {
    x[i][&quot;t&quot;] = &quot;21&quot;
    console.log(x)
    isKeyExists = true;
    break
  }  
}

if (!isKeyExists) {
  x.push({
      &quot;v&quot;: &quot;22&quot;,
      &quot;t&quot;: &quot;77&quot;
  })
}

console.log(x)

In the code above, you just add the object after you have exited the loop.

答案2

得分: -1

你的代码不起作用,因为你试图访问x数组中的对象中的d键,但x数组中的任何对象都不存在d键。你可以通过在尝试访问之前检查当前对象中是否存在d键来修复这个问题。你可以使用hasOwnProperty()方法来实现这一点。

以下是已校正的代码:

x = [{
  "s": "23",
  "t": "41"
}, {
  "e": "29",
  "t": "87"
}]

for (var i = 0; i < x.length; i++) {
  if (x[i].hasOwnProperty("d")) {
    x[i]["t"] = "21";
    console.log(x);
    break;
  } else if (i === x.length - 1) {
    x.push({
      "v": "22",
      "t": "77"
    });
    console.log(x);

    // 循环结束
  }
}
英文:

Your code is not working because you are trying to access the d key in the x array, but the d key does not exist in any of the objects in the x array. You can fix this by checking if the d key exists in the current object before you try to access it. You can do this by using the hasOwnProperty() method.

Here is the corrected code:

x = [{
  &quot;s&quot;: &quot;23&quot;,
  &quot;t&quot;: &quot;41&quot;
}, {
  &quot;e&quot;: &quot;29&quot;,
  &quot;t&quot;: &quot;87&quot;
}]

for (var i = 0; i &lt; x.length; i++) {
  if (x[i].hasOwnProperty(&quot;d&quot;)) {
    x[i][&quot;t&quot;] = &quot;21&quot;
    console.log(x)
    break
  } else if (i === x.length - 1) {
    x.push({
      &quot;v&quot;: &quot;22&quot;,
      &quot;t&quot;: &quot;77&quot;
    })
    console.log(x)

    //loop finised
  }
}

huangapple
  • 本文由 发表于 2023年5月20日 20:36:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76295265.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定