简体   繁体   English

数组Array中的Javascript嵌套过滤器

[英]Javascript nested filters in Array of arrays

I have an array of objects in this format: 我有这种格式的对象数组:

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'}
            ]
        }...
    ];

I have a tmp array which consists of objects from the full array: 我有一个tmp数组,它由完整数组中的对象组成:

 var tmp_list =  [
        {
            "pid": 2,
            "items":[
                {"item_id": '33'},
                {"item_id": '22'},
                {"item_id": '65'}
            ]
        }, {....}

I would like to filter out objects from the full list where at least one of selectedIDs values appears in the object's item's id array 我想过滤掉完整列表中的对象,其中至少有一个selectedIDs值出现在对象的项目的id数组中

var selectedIDs = {'1', '9', '45', ....};

and then add them to the tmp list. 然后将它们添加到tmp列表中。

I tried using filters but I failed to figure it out completely. 我尝试使用过滤器,但我没有完全搞清楚。

Thank you. 谢谢。

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);
                 });

You could use a hash table for the selectedID and use it for fast filtering. 您可以为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 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); 

First of all, this line in your question is wrong 首先,你问题中的这一行是错误的

var selectedIDs = {'1', '9', '45', ....}; var selectedIDs = {'1','9','45',....};

You cannot declare arrays using {} . 您无法使用{}声明数组。 Instead use [] 而是使用[]

For your problem you can use a pure functional approach using Array#filter and Array#some methods to get your desired result as below: 对于您的问题,您可以使用Array#filterArray#的一些 纯函数方法来获得您想要的结果,如下所示:

 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)); 

You may do like this; 你可能喜欢这样;

 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); 

I think this could be a solution. 我认为这可能是一个解决方案。

 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) 

array1
array2

array1.filter(el=>{
   return array2.filter(el2=>{
          return el.id == el2.id
     })
})

It will take array1 and run it against the second one ... for each value. 这将采用array1并针对第二个运行它...为每个值。

Spread operator should simplify some code: 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); 

Looking at your job 1. There is nothing like market 看看你的工作1.没有什么比market

obj.items.forEach(function (item) { if (item.item_id === id) { console.log('found'); return true; } return false; obj.items.forEach(function (item) { if (item.item_id === id) { console.log('found'); return true; } return false;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM