What the question says...
Does jQuery have any methods that will allow you to query a mult-dimensional array of objects in a similar fashion as it does with the DOM.
I just wrote this.. I think it works properly but it could definitely get cleaned up :)
function findMatchingObjects(obj, member, value){
var final = new Array();
var temp = new Array();
for(var p in obj){
if (typeof obj[p] == 'object' ) {
temp = findMatchingObjects(obj[p], member, value);
if (temp.length == 1)
final.push(temp[0]);
}
if (p == member && obj[p] == value) {
final.push(obj);
}
}
alert(final.length);
return final;
}
Use it like so:
var moo ={baz: 1, boo: 2, bar:{c1: 3, c2: 4, c3:{t:true, f:false, baz:1}},d: 11};
var foo = findMatchingObjects(moo, "baz", 1);
// did it work?
console.log(foo);
Returns an array of object (or sub-objects) that match the member-value pair. In this case, foo
contains both moo
and c3
since both of the objects contain a baz = 1
pair.
Making the function look and feel like a jQuery selector is just a matter of syntactic sugar.