[英]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.