search nested array

Jun 28, 2010 at 5:09 PM

Hi just checking out jslinq for the first time -

Wondering how a search would be done to return results based on bookIDs given the sample data below i.e. select all entries that have a bookID of 1002

Samples.People = [
    { ID: 1, FirstName: "Chris", LastName: "Pearson", BookIDs: [1001, 1002, 1003] },
    { ID: 2, FirstName: "John", LastName: "Ronald", BookIDs: [4001, 4002, 4003] },
    { ID: 3, FirstName: "Steve", LastName: "Pinkerton", BookIDs: [1001, 1002, 1003] },
    { ID: 4, FirstName: "Bernard", LastName: "Sutherland", BookIDs: [1001, 2002, 3003] },
    { ID: 5, FirstName: "Kate", LastName: "Pinkerton", BookIDs: [4001, 3002, 2003] }
];

Coordinator
Oct 25, 2010 at 1:10 PM

You just need to write a nested query. Below is the example that I just added to the SDK Samples.

var sample = JSLINQ(Samples.People).
            Where(function (item, index) {
                return JSLINQ(item.BookIDs)
                    .Where(function (item) { return item == 1002 })
                    .Count() > 0;
            });

 

Sorry for the long delayed reply.