I have an object,
var _data = {
teacher : {
name : "Mack",
subject : "Maths"
},
Student : [{
name : "Alex",
stud_id : 1
},{
name : "Jack",
stud_id : 2
},{
name : "Mark",
stud_id : 3
}]
}
I want to create index on stud_id, how can I create?
What I would do that is to have two separate object stores inside of your database, one which represents teachers and one which represents students.
Then I'd store a teacher object like:
{
name: 'John Smith',
subject: 'Maths',
students: [1,2,3,4,5]
}
Then I'd have a store for students like:
{
id: 1,
name: 'Jane Doe'
}
I'd then have an id key path in both stores (autogenerated or not, depending on whether it syncs somewhere) so you have a unique identifier of what the data is.
Finally you can create an index on the students property of a teacher and set the multiEntry flag to true so that it is known to be an array and you can query the object store on it as an array, see here for more info on multiEntry indexes.
Note: At time of writing IE10 & IE11 do not support multiEntry indexes in IndexedDB.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With