英文:
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 = [{
"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)
//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 = [{
"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.
答案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 = [{
"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)
//loop finised
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论