JavaScript对象我有一个格式的Javascript对象像下面操纵以下划线
"items":
{
"Groups":[
{
"title":"group 1",
"SubGroups":[
{
"title":"sub1",
"id" : "1",
"items":[
{
"title":"Ajax request 1",
},
{
"title":"Ajax request 2",
}
]
},
{
"title":"sub2",
"id" : "2",
"items":[
{
"title":"Ajax request 3",
},
{
"title":"Ajax request 4",
}
]
}
]
}
]
有N“组”,N“亚组”和n“项目”。
我想要做的第一件事就是根据id从特定组中获取所有项目。这是通过使用来实现:
_.each(items.Groups, function(o) {
result = _.where(o.SubGroups, {
'id': '1'
});
});
返回
"items":[{"title":"Ajax request 1",},{"title":"Ajax request 2",}]
然后,我想要得到的数据的其余部分,不包括我刚才检索到的项目和父组。
我尝试这样做:
_.each(items.Groups, function(o) {
arr = _.without(o.SubGroups, _.findWhere(o.SubGroups, {id: '2'}));
});
但这仅返回我的项目是这样的:
{
"title":"sub2",
"id" : "2",
"items":[{"title":"Ajax request 3"},{"title":"Ajax request 4",}]
}
,而我需要的是这样的:
"items":
{
"Groups":[
{
"title":"group 1",
"SubGroups":[
{
"title":"sub2",
"id" : "2",
"items":[
{
"title":"Ajax request 3",
},
{
"title":"Ajax request 4",
}
]
}
]
}
]