我使用lokijs来存储我需要使用的一些数据。我的数据如下所示。作为lokijs的新手,我一直在努力寻找从item_part_option_groups
中获取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": [
]
}, ]
}]
}]
}
}
答案 0 :(得分:2)
如果我理解正确,你就会过度复杂化你的生活。
LokiJS将为您的对象添加一些属性(如id $loki
和meta
对象),但基本上它将返回对此略微修改的原始对象的引用(除非您使用{{ 1}}构造集合时的选项。)
这意味着 - 要记录clone
- 您只需要这样做:
item_part_option_groups
您在console.log(data.item_part_option_groups)
中撰写的查询将在find
集合中查找partData
属性等于part
的对象。显然,这不是你想要的。一旦对象进入集合 - 这发生在这里:
item_part_option_groups
该记录是普通的js对象,因此如果您将其更改为:
partData.insert(data)
你只需要引用var record = partData.insert(data)
来获取你想要的财产。
所有这些与LokiJS几乎没有关系:在我看来,好像你正试图获得一个"部分"对象,只能通过map-reduce或其他更复杂的变换来完成。