2015-11-03 155 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>';