Reputation: 3071
I have a JSON object :
[{"box":1,"parent":[],"child":[{"boxId":2}]},{"box":2,"parent":[{"boxId":1}],"child":[]}]
I have a requirement where in I would like to check whether my JSON object has particular box; if yes then check if it has particular child.
eg: check if box 1 exists
if yes then
check if it has child
if yes then
check if it has child boxId=2
How do I do that in javascript/ jquery?
This is how I tried:
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;
});
}
}
This doesn't seem to work as even after I return false it still continues to go in loop. I would like to break the loop if i find a match.
Any suggestion?
Upvotes: 2
Views: 2651
Reputation: 386560
You need to iterate over all arrays, you need.
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());
Another solution features a more generic approach, with a given object, which acts as a pattern for the needed items.
[
{ 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>');
Upvotes: 2
Reputation: 74738
You can make use of recursion. call the same function recursively to check if the element you need is really exist in the array or not.
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>';
Upvotes: 1
Reputation: 11614
I think these will help.
for(var i=DependantArr.length;i>=0;i--) {
return DependantArr[i].box == 1 && DependantArr[i].child.length!=0 &&
DependantArr[i].child[0].boxId==2;
}
Upvotes: -1
Reputation: 1440
If this is the only case you need to check, you can use this:
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;
}
});
Upvotes: -1
Reputation: 28387
Create a function that will call the filter
on your array and return it. The returned value would be an array containing the found object(s) which match(es) your condition(s).
Demo Snippet: (check the console)
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]);
Upvotes: 1
Reputation: 4489
Try this
data.forEach(function (el) {
Object.keys(el).forEach(function (property) {
if (el[property] === 'your value to check') {
// do whatever you need here
}
});
});
Upvotes: 0