Reputation: 1397
I'm using lokijs to store some data that I will need to work with. My data is shown below. As a newbie to lokijs I've been struggling to find a way to get item_part_option_groups
array out of part
.
partDb = new loki('part.json');
partData = partDb.addCollection('part');
$.get(urlHelper.createUrl('item/parts', 'part', config.partId + '.json'), function(data){
partData.insert(data);
var data = partData.find({'part':'item_part_option_groups'});
console.log(data);
});
{
"part": {
"item_part_id": 10,
"name": "Sleeve",
"has_validation": 0,
"description": "",
"is_disabled": "1",
"created": "2015-07-22T23:55:09+0000",
"updated": null,
"item_part_option_groups": [{
"item_part_option_group_id": 10,
"order_index": 0,
"description": "",
"item_part_id": 10,
"created": "2015-07-22T23:55:39+0000",
"updated": null,
"item_part_options": [{
"item_part_option_id": 8,
"name": "Sleeve Type",
"is_conform_value": 0,
"has_validation": 1,
"item_part_option_type_id": 3,
"created": "2015-07-22T23:57:24+0000",
"updated": null,
"item_part_option_values": [{
"value": "Short Sleeve",
"order_index": 0,
"item_part_option_id": 8,
"item_part_option_value_id": 7,
"item_part_option_includes": [
]
}, {
"value": "Long Sleeve",
"order_index": 0,
"item_part_option_id": 8,
"item_part_option_value_id": 8,
"item_part_option_includes": [
]
}, ]
}]
}]
}
}
Upvotes: 2
Views: 2299
Reputation: 2773
if I understand correctly, you're over-complicating your life.
LokiJS will add some properties to your object (like the id $loki
and a meta
object) but basically it will return a reference to this slightly modified original object (unless you're using the clone
option when constructing the collection).
This means that - to log item_part_option_groups
- you only need to do:
console.log(data.item_part_option_groups)
The query your wrote in find
will look for an object in the partData
collection whose part
property equals to item_part_option_groups
. Obviously that's not what you want. Once the object is in the collection - which happens here:
partData.insert(data)
The record is a plain js object, so if you change it to:
var record = partData.insert(data)
you simply need to refer to record['item_part_option_groups']
to grab the property you want.
All this has little to do with LokiJS however: it seems to me as if you're trying to obtain a "partial" object, which can only be done with a map-reduce or other more complex transforms.
Upvotes: 1