![](/img/trans.png)
[英]How can I iterate through array of deeply nested objects in javascript?
[英]How can I iterate through a javascript array and call a function that updates the same array?
我有這個數組:
var test.qs =
[
{"answer":null,
"testQuestionId":710
"synchronized":true},
{"answer":null,
"testQuestionId":711
"synchronized":false
]
我想在所有具有syncronized屬性等於false的數組對象上調用一個函數,如下所示:
if (!test.qs[x].synchronized) {
httpPutTestQuestion(number)
.success(function (data) {
test.qs[x].synchronized = true;
})
}
但是我該怎么辦,就好像我將其放入for循環中一樣,那么當函數返回時,我將仍然沒有值x?
我認為這應該為您工作。
test.qs.forEach(function (q) {
if (!q.synchronized) {
httpPutTestQuestion(number)
.success(function (data) {
q.synchronized = true;
})
}
})
為什么不使用普通的舊循環,像這樣
var i;
for (i = 0; i < qs.length; i += 1) {
if (qs[i].synchronized === false) {
httpPutTestQuestion(number)
.success(function (data) {
test.qs[x].synchronized = true;
})
};
}
}
您可以使用length
屬性獲取數組的length
。 另請注意,JavaScript中的變量名稱不能具有.
在他們中。 所以,
var test.qs = ....
將失敗。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.