简体   繁体   中英

Using Xpath selectors to extract / combine data from Javascript Objects?

I'm quite fond of CakePHP Set class , it comes with a few awesome tools i now love. I use it mainly to extract or combine arrays.

I was wondering if there was a way to do just that with javascripts Objects/Arrays of data.

// Desired Usage:
var users = {User: {0:{id:1,name:'A'},1:{id:2,name:'B'}}}
var results = $.extract('/User/id', users);
// results returns:
// {0:1,1:2};

// /User[id>2][<5]  Selects all Users with an id > 2 but < 5

It could support on jQuery or maybe just Sizzle.

Do i have to develop thoses functions from zero or is there already some native/plugin xpath selector/extractor support out there ? Can Sizzle do this ?

Thanks a lot!

Look for that library http://code.google.com/p/jsonpath/
Not sure, whether it could process request exactly in this syntax: User[id>2] [<5] ,
but it's rather powerful library and should have similar feature.

Even if there's no function for request parts like "[<5]", you may call

.slice(0, 5);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM