2015-11-03 158 views
1

我有一個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它仍然繼續在循環中去。我想打破循環,如果我找到一場比賽。

有什麼建議嗎?

+1

使用普通的for循環的話,你不能脫離使用'.MAP /回報()'。由於return是用於當前的回調,而不是整個操作。 –

回答

2

您需要遍歷所有的數組,你需要的。

var array = [{ "box": 1, "parent": [], "child": [{ "boxId": 2 }] }, { "box": 2, "parent": [{ "boxId": 1 }], "child": [] }]; 
 

 
function check() { 
 
    var found = false; 
 
    array.some(function (a) { 
 
     if (a.box === 1) { 
 
      Array.isArray(a.child) && a.child.some(function (b) { 
 
       if (b.boxId === 2) { 
 
        found = true; 
 
        return true; 
 
       } 
 
      }); 
 
      return true; 
 
     } 
 
    }); 
 
    return found; 
 
} 
 

 
document.write(check());

另一種解決方案設有一個更通用的方法,用給定的對象,其作爲用於需要的物品的圖案。

[ 
    { condition: { box: 1 }, nextKey: 'child' }, 
    { condition: { boxId: 2 } } 
] 

var array = [{ "box": 1, "parent": [], "child": [{ "boxId": 2 }] }, { "box": 2, "parent": [{ "boxId": 1 }], "child": [] }]; 
 
    
 
function check(array, conditions) { 
 

 
    function c(a, index) { 
 
     var el = conditions[index], 
 
      k = Object.keys(el.condition), 
 
      v = el.condition[k], 
 
      found = false; 
 

 
     return Array.isArray(a) && 
 
      a.some(function (b) { 
 
       if (b[k] === v) { 
 
        found = true; 
 
        if (conditions.length > index + 1) { 
 
         found = c(b[el.nextKey], index + 1); 
 
        } 
 
        return true; 
 
       } 
 
      }) && 
 
      found; 
 
    } 
 
    return c(array, 0); 
 
} 
 

 
document.write(check(array, [{ condition: { box: 1 }, nextKey: 'child' }, { condition: { boxId: 2 } }])+'<br>'); 
 
document.write(check(array, [{ condition: { box: 2 }, nextKey: 'parent' }, { condition: { boxId: 1 } }]) + '<br>');

-1

我認爲這些將有所幫助。

for(var i=DependantArr.length;i>=0;i--) { 
        return DependantArr[i].box == 1 && DependantArr[i].child.length!=0 && 
    DependantArr[i].child[0].boxId==2; 

      } 
+0

檢查它是否有孩子boxId = 2,並且你的代碼中有很多錯誤:'DependantArr [i] .box。長度' – Ziki

+0

@Ziki ..更新了它。謝謝 –

-1

如果這是你需要檢查的唯一案例,你可以使用這個:

var DependantArr = [{"box": 1, "parent": [], "child": [{"boxId": 3}]}, {"box": 2, "parent": [{"boxId": 1}], "child": []}]; 
var $hasDependancy = DependantArr.some(function(thisBox) { 
    if ((thisBox.box === 1) && (thisBox.hasOwnProperty('child'))) { 
     return thisBox.child.filter(function(thisChild) { 
      return thisChild.boxId === 2; 
     }).length > 0; 
    } 
}); 
0

試試這個

data.forEach(function (el) { 
Object.keys(el).forEach(function (property) { 
    if (el[property] === 'your value to check') { 
     // do whatever you need here 
    } 
    }); 
}); 
1

創建將打電話給你的陣列上的filter並返回它的功能。返回的值將是一個包含匹配條件的找到的對象的數組。

演示片段:檢查控制檯

var json = [{"box":1,"parent":[],"child":[{"boxId":2}]},{"box":2,"parent":[{"boxId":1}],"child":[]}]; 
 

 
function search(id, childId) { 
 
    return json.filter(function(obj) { 
 
     if ((obj.box == id) && (obj.child) && (obj.child.length > 0)) { 
 
      return obj.child.filter(function(child) { 
 
       return (child.boxId == childId); 
 
      }); 
 
     } 
 
    }); 
 
} 
 

 
console.log(search('1', '2')[0]); 
 
console.log(search('2', '2')[0]);

+1

「檢查它是否有孩子boxId = 2」 – Ziki

+0

謝謝@Ziki。我完全錯過了那部分。更新。 – Abhitalks

1

您可以使用遞歸的。遞歸調用相同的函數來檢查你需要的元素是否真的存在於數組中。

var json = [{"box":1,"parent":[],"child":[{"boxId":2}]},{"box":2,"parent":[{"boxId":1}],"child":[]}]; 
 

 
var found = false; 
 
function validateJson(data, box, boxId){ 
 
    for(key in data){ 
 
    if((data[key].constructor === Object || data[key].constructor === Array) && (key !== box && data[key] !== 1 || key !== boxId && data[key] !== 2)){ 
 
     arguments.callee(data[key]); // <---calls the same function again. 
 
    } else { 
 
     found = true; // true only in the case of if "box":1 and "boxId" : 2 
 
    } 
 
    } 
 
    return found; 
 
} 
 
var result = validateJson(json, "box", "boxId"); 
 
document.body.innerHTML = '<pre> found : '+JSON.stringify(result) + '</pre>';