我有一個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是用於當前的回調,而不是整個操作。 –