I\'ve just started using jquery and I\'m really enjoying using selectors. It occurs to me that the idiom would be a very nice way to traverse object trees (e.g., JSON query resu
Well, I'd personally say that pure object access looks better than jQuery-like queries. One thing that would be neat would be slicing and other filtering techniques.
If you really wanted to play with object access queries, the following would be some possibilities (think XPath):
var obj = {
foo: 1,
bar: 2,
child: {
foo: {
baz: [3, {a: 1}, {a: 2, b: 3}]},
bar: {
baz: [42, {a: 123}, {a: -1}]},
baz: null}};
// Implicitly start in the Global object, unless a context is provided.
// Keys in JavaScript objects are essentially stored in order (not valid for
// *all* flavors, but it's close to standard. So you could do slicing on keys.)
// Selects (does not return them)
// obj.child.foo.baz[1].a
// obj.child.foo.baz[2].a
// obj.child.bar.baz[1].a
// obj.child.bar.baz[2].a
// Then performs an aggregate that returns value 125.
$('obj.child[:2].baz[1:].a').sum()
// Selects obj.foo, obj.bar, obj.child.foo.baz[0] and obj.child.bar.baz[0]
$('obj *[typeof(number)]')
// Selects obj.foo and obj.child.foo
$('obj foo')
// Other possible methods: delete(), set(), get() (as an array of values),
// min(), max(), avg(), merge() etc etc.
In the end though, I don't see how this is very useful. But eh, it's an idea I guess =)