数组数组中的Javascript嵌套筛选器
Javascript nested filters in Array of arrays
我有一个对象数组,格式如下:
var full_list = [
{
"pid": 1,
"items":[
{"item_id": '9'},
{"item_id": '10'},
{"item_id": '12'}
]
},
{
"pid": 2,
"items":[
{"item_id": '33'},
{"item_id": '22'},
{"item_id": '65'}
]
}...
];
我有一个tmp数组,它由来自完整数组的对象组成:
var tmp_list = [
{
"pid": 2,
"items":[
{"item_id": '33'},
{"item_id": '22'},
{"item_id": '65'}
]
}, {....}
我想从完整列表中筛选出对象,其中至少有一个selectedID值出现在对象的id数组中
var selectedIDs = {'1', '9', '45', ....};
然后将它们添加到tmp列表中。
我试着使用过滤器,但没能完全弄清楚。
谢谢。
selectedIDs.forEach(function(id) {
var tmp = full_list.filter(function (obj) {
obj.items.forEach(function (item) {
if (item.id === id) {
console.log('found');
}
});
});
tmp_list.push(tmp);
});
首先,问题中的这一行是错误的
var selectedID={‘1’,‘9’,‘45’,……};
不能使用{}
声明数组。而是使用[]
对于您的问题,您可以使用纯函数方法,使用Array#filter和Array#一些方法来获得您想要的结果,如下所示:
var full_list = [
{
"pid": 1,
"items":[
{"item_id": '9'},
{"item_id": '10'},
{"item_id": '12'}
]
},
{
"pid": 2,
"items":[
{"item_id": '33'},
{"item_id": '22'},
{"item_id": '67'}
]
},
{
"pid": 9,
"items":[
{"item_id": '33'},
{"item_id": '22'},
{"item_id": '65'}
]
},
{
"pid": 7,
"items":[
{"item_id": '7'},
{"item_id": '22'},
{"item_id": '65'}
]
}
];
var tmp_list = [
{
"pid": 2,
"items":[
{"item_id": '7'},
{"item_id": '22'},
{"item_id": '65'}
]
}
];
function filterResult (selectedItems) {
return full_list.filter(function (process) {
return process.items.some(function(item){
return selectedItems.indexOf(item.item_id) > -1;
});
});
}
var selectedItems = ['9', '7', '22', '10'];
tmp_list = tmp_list.concat(filterResult(selectedItems))
console.log(tmp_list);
function flattenResults(list, selections) {
return list.reduce(function (accumulator, current) {
var res = current.items.filter(function(item){
return (selections.indexOf(item.item_id) > -1
&& checkIfAlreadyExist());
function checkIfAlreadyExist () {
return accumulator.every(function (k) {
return k.item_id !== item.item_id;
});
}
});
return accumulator.concat(res);
}, []);
}
console.log(flattenResults(full_list, selectedItems));
您可以这样做;
var full_list = [
{
"pid": 1,
"items":[
{"item_id": '9'},
{"item_id": '10'},
{"item_id": '12'}
]
},
{
"pid": 2,
"items":[
{"item_id": '33'},
{"item_id": '22'},
{"item_id": '65'}
]
}
],
selectedIDs = ['1', '9', '45'],
tempList = [];
tempList.push(full_list.filter(f => f.items.some(o => selectedIDs.includes(o.item_id))));
console.log(tempList);
您可以为selectedID使用哈希表,并将其用于快速筛选。
var full_list = [{ "pid": 1, "items": [{ "item_id": '9' }, { "item_id": '10' }, { "item_id": '12' }] }, { "pid": 2, "items": [{ "item_id": '33' }, { "item_id": '22' }, { "item_id": '65' }] }],
tmp_list,
selectedIDs = ['1', '9', '45'],
selected = Object.create(null);
selectedIDs.forEach(function (a) {
selected[a] = true;
});
tmp_list = full_list.filter(function (a) {
return !a.items.some(function (b) {
return selected[b.item_id];
});
});
console.log(tmp_list);
ES6
var full_list = [{ "pid": 1, "items": [{ "item_id": '9' }, { "item_id": '10' }, { "item_id": '12' }] }, { "pid": 2, "items": [{ "item_id": '33' }, { "item_id": '22' }, { "item_id": '65' }] }],
tmp_list,
selectedIDs = ['1', '9', '45'],
selected = Object.create(null);
selectedIDs.forEach(a => selected[a] = true);
tmp_list = full_list.filter(a => !a.items.some(b=> selected[b.item_id]));
console.log(tmp_list);
array1
array2
array1.filter(el=>{
return array2.filter(el2=>{
return el.id == el2.id
})
})
它需要数组1,并与第二个数组进行比较。。。对于每个值。
我认为这可能是一个解决方案。
var full_list = [
{
"pid": 1,
"items": [
{ "item_id": '9' },
{ "item_id": '10' },
{ "item_id": '12' }
]
}, {
"pid": 2,
"items": [{
"item_id": '33'
}, {
"item_id": '22'
}, {
"item_id": '65'
}]
}];
var selectedIDs = ['33', '3'];
var tempList = [];
full_list
.filter(item =>
item.items
.some(i => selectedIDs.indexOf(i.item_id) != -1)
).forEach(item => tempList.push(item));
console.log(tempList)
查看您的工作1.没有什么能比得上market
obj.items.forEach(function (item) {
if (item.item_id === id) {
console.log('found');
return true;
}
return false;
});
Spread operator
应该简化一些代码:
var full_list = [
{
"pid": 1,
"items": [
{ "item_id": '9' },
{ "item_id": '10' },
{ "item_id": '12' }
]
}, {
"pid": 2,
"items": [{
"item_id": '33'
}, {
"item_id": '22'
}, {
"item_id": '65'
}]
}];
var selectedIDs = ['65', '6'];
var tempList = [];
tempList = [...tempList,
...full_list.filter(item => item.items.some(i => selectedIDs.includes(i.item_id)))
];
console.log(tempList);
相关文章:
- 如何从另一个带下划线的数组中筛选带元素的数组
- 如何筛选对象的数组属性
- 根据多个值筛选数组
- 如何按对象数组中的数组属性进行筛选
- 要筛选的数组的管道值的含义:$select.search in angular
- 如何筛选对象数组
- 具有绑定与未绑定函数的数组筛选器
- 角度筛选器 - 根据值数组筛选 JSON 结果
- 基于条件数组的数组筛选器
- 根据数组筛选字符串数组
- 按键数组筛选对象
- 基于索引数组筛选数组
- 通过字符串数组筛选对象数组
- 针对所有其他数组筛选数组
- Javascript:根据数组筛选键后,在对象中找到一个值最大的键
- 使用值数组筛选多个属性的值
- 使用另一个对象数组筛选对象数组
- 数组筛选器的异步或承诺条件
- 通过另一个整数数组筛选对象数组
- 按搜索查询进行数组筛选