我有一个JSON对象:遍历JSON对象,并检查是否特定对象存在
[{"box":1,"parent":[],"child":[{"boxId":2}]},{"box":2,"parent":[{"boxId":1}],"child":[]}]
我有一个要求,凡在我想检查我的JSON对象是否具有特定的盒子;如果是,那么检查它是否有特定的孩子。
eg: check if box 1 exists if yes then check if it has child if yes then check if it has child boxId=2
如何做到这一点在JavaScript/jQuery的?
这是我尝试:
var DependantArr=myJSON;
var $hasDependancy;
DependantArr.map(function (boxes) {
if (boxes.box == 2) {
if (boxes.child.length != 0) {
boxes.child.map(function (child) {
$hasDependancy = true;
return false;
});
}
}
这似乎并没有因为即使工作后,我返回false它仍然继续在循环中去。我想打破循环,如果我找到一场比赛。
有什么建议吗?
使用普通的for循环的话,你不能脱离使用'.MAP /回报()'。由于return是用于当前的回调,而不是整个操作。 –