Javascript 过滤对象内部对象上的JSON对象
我有一些数据看起来像:Javascript 过滤对象内部对象上的JSON对象,javascript,json,Javascript,Json,我有一些数据看起来像: var items = [ { "id" : 1, "title" : "this", "groups" : [ {"id" : 1, "name" : "groupA"}, {"id" : 2, "name" : "groupB"} ] }, { "id" : 2, "title" : "that", "groups" : [ {"id" :
var items = [
{ "id" : 1,
"title" : "this",
"groups" : [
{"id" : 1,
"name" : "groupA"},
{"id" : 2,
"name" : "groupB"}
]
},
{ "id" : 2,
"title" : "that",
"groups" : [
{"id" : 1,
"name" : "groupA"},
{"id" : 3,
"name" : "groupC"}
]
},
{ "id" : 3,
"title" : "other",
"groups" : [
{"id" : 3,
"name" : "groupC"},
{"id" : 2,
"name" : "groupB"}
]
}]
我想根据组id进行筛选,但我甚至在访问组id时遇到问题-item.group返回整个对象,而执行任何其他操作(即item.groups.id)都会返回null或未定义的值
任何关于如何做到这一点的帮助都将是巨大的。本质上,我想过滤数组以包含特定组中的所有项
谢谢试试这个:
group3Items = items.filter(item => item.groups.findIndex(group => group.id==3) > -1)
试试这个:
group3Items = items.filter(item => item.groups.findIndex(group => group.id==3) > -1)
使用:
id
的对象以下代码有两个输出:
3
的项目const项=[{
“id”:1,
“标题”:“本”,
“团体”:[{
“id”:1,
“名称”:“groupA”
},
{
“id”:2,
“名称”:“groupB”
}
]
},
{
“id”:2,
“标题”:“该”,
“团体”:[{
“id”:1,
“名称”:“groupA”
},
{
“id”:3,
“名称”:“groupC”
}
]
},
{
“id”:3,
“标题”:“其他”,
“团体”:[{
“id”:3,
“名称”:“groupC”
},
{
“id”:2,
“名称”:“groupB”
}
]
}
];
const filteredRemovingGroups=items.map(item=>({
…项目,
组:item.groups.filter(subItem=>subItem.id==3)
}));
const filterAndFlatten=items.map(item=>
item.groups.filter(subItem=>subItem.id==3)
).reduce((combinedArr,arr)=>[…combinedArr,…arr],])
log('filteredRemovingGroups',filteredRemovingGroups);
log('filterAndFlatten',filterAndFlatten')代码>使用:
创建一个新数组,将数组中的每个项映射到一个新对象
使用*克隆每个项目,并使用
仅保留具有正确id
的对象
*需要像babel或typescript这样的transpiler吗
或
如果要展平结构,则可以使用组合阵列
以下代码有两个输出:
一个保持原始结构,但过滤掉组中id为3
的项目
一个将结构展平,只输出一个阵列
const项=[{
“id”:1,
“标题”:“本”,
“团体”:[{
“id”:1,
“名称”:“groupA”
},
{
“id”:2,
“名称”:“groupB”
}
]
},
{
“id”:2,
“标题”:“该”,
“团体”:[{
“id”:1,
“名称”:“groupA”
},
{
“id”:3,
“名称”:“groupC”
}
]
},
{
“id”:3,
“标题”:“其他”,
“团体”:[{
“id”:3,
“名称”:“groupC”
},
{
“id”:2,
“名称”:“groupB”
}
]
}
];
const filteredRemovingGroups=items.map(item=>({
…项目,
组:item.groups.filter(subItem=>subItem.id==3)
}));
const filterAndFlatten=items.map(item=>
item.groups.filter(subItem=>subItem.id==3)
).reduce((combinedArr,arr)=>[…combinedArr,…arr],])
log('filteredRemovingGroups',filteredRemovingGroups);
log('filterAndFlatten',filterAndFlatten')代码>您可以这样做
var items = [{
"id": 1,
"title": "this",
"groups": [{
"id": 1,
"name": "groupA"
}, {
"id": 2,
"name": "groupB"
}]
}, {
"id": 2,
"title": "that",
"groups": [{
"id": 1,
"name": "groupA"
}, {
"id": 3,
"name": "groupC"
}]
}, {
"id": 3,
"title": "other",
"groups": [{
"id": 3,
"name": "groupC"
}, {
"id": 2,
"name": "groupB"
}]
}]
// A filter function to filter out the matched value
function filterArray(array, val) {
return array.filter(function(elem) {
return elem.id === val; // filtering by Id
})
}
// first filtering the original items array, & will get the object where id is 1
var obj = filterArray(items, 1);
//the previous result will return an array,
//so doing obj[0] which will give the first index.
//obj[0].groups will be an array again
var filterGroup = filterArray(obj[0].groups,1) // will be an array which contains the matched group
你可以这样做
var items = [{
"id": 1,
"title": "this",
"groups": [{
"id": 1,
"name": "groupA"
}, {
"id": 2,
"name": "groupB"
}]
}, {
"id": 2,
"title": "that",
"groups": [{
"id": 1,
"name": "groupA"
}, {
"id": 3,
"name": "groupC"
}]
}, {
"id": 3,
"title": "other",
"groups": [{
"id": 3,
"name": "groupC"
}, {
"id": 2,
"name": "groupB"
}]
}]
// A filter function to filter out the matched value
function filterArray(array, val) {
return array.filter(function(elem) {
return elem.id === val; // filtering by Id
})
}
// first filtering the original items array, & will get the object where id is 1
var obj = filterArray(items, 1);
//the previous result will return an array,
//so doing obj[0] which will give the first index.
//obj[0].groups will be an array again
var filterGroup = filterArray(obj[0].groups,1) // will be an array which contains the matched group
我一粘贴溶液就注意到了这一点。固定:)也结帐。结果是:group3Items=items.filter(item=>item.groups.includes(group=>group.id==3))
。但请注意,您需要一个polyfill,因为包含是es2017的一项功能。是的,这就是我选择findIndex:)的原因。我一粘贴解决方案就注意到了这一点。固定:)也结帐。结果是:group3Items=items.filter(item=>item.groups.includes(group=>group.id==3))
。但请注意,您需要一个polyfill,因为包含是es2017的一项特性。是的,这就是为什么我选择findIndex:)。数据结构不好。您不应该在每个项中都有组信息。组数组。组应该有一个单独的数组,或者是一个映射和项。组数组应该只有组索引或对组项的引用。这将减少JSON的大小,使您的数据更易于管理。数据结构不好。您不应该在每个项中都有组信息。组数组。组应该有一个单独的数组,或者是一个映射和项。组数组应该只有组索引或对组项的引用。这将减少JSON的大小,并使您的数据更易于管理。