This project has moved and is read-only. For the latest updates, please go here.

is it possible to retrieve and filter the array of an array?

Oct 12, 2012 at 10:52 PM
Edited Oct 12, 2012 at 10:56 PM

I have a record in indexedDB:

Group:

{"id":1,"contact":[{"id":1,"name":"Christopher"},{"id":2,"name":"Emmy"},{"id":3,"name":"Annick"}],"title":"group1"}

 

is it possible to retrieve and filter the array of an array using linq2indexedDB?

in my example, i want to retrieve only the contact having -> {"id":2,"name":"Emmy"}

Oct 14, 2012 at 12:20 PM

Try the following:

db.linq.from("objectstore").where("contact.id").equals(2).select(["contact.id", "contact.name"]);

Oct 14, 2012 at 6:49 PM

it does not work.. the output is: []

and when i tried without the where condition :

db.linq.from("group").select(["contact.id", "contact.name"]).then(function(data){
     alert(JSON.stringify(data));
});

i hv got this : [{"contact":{}}]

Here is the test that i tried:

//Structure of contact
function ContactVO(){
    this.id =0;
    this.name;
}


//Structure of group
function GroupVO(){
    this.id = 0;
    this.contact= new Array();
    this.title;
}


//Create contact 1
var c1 = new ContactVO();
c1.id=1;
c1.name='Christopher';


//Create contact 2
var c2 = new ContactVO();
c2.id=2;
c2.name='Emmy';


//Create contact 3
var c3 = new ContactVO();
c3.id=3;
c3.name='Annick';

var g1 = new GroupVO();
g1.id=1;
g1.title='group1';

var contacts = new Array();
contacts.push(c1);
contacts.push(c2);
contacts.push(c3);
g1.contact = contacts;  //a group can contain many contacts, for this example, there are three contacts

 var db = $.linq2indexedDB("group", null, true);
  db.linq.from("group").insert(d1);

Any idea?

Oct 17, 2012 at 8:07 AM

I don't know if it was a typo or not, but when you insert you insert d1 which isn't defined.

Nov 2, 2012 at 9:59 PM
Edited Nov 2, 2012 at 10:07 PM

Sorry, it s not d1, but g1

The solution that i have found is to use  "filter" for the array contact

db.linq.from("group").select(["contact"]).then(function(data){

data[0].contact.filter(function (e) {
  return e.id==2
});


});

Anyway, thanks for your help