数组数组中的Javascript嵌套筛选器

Javascript nested filters in Array of arrays

本文关键字:数组 筛选 嵌套 Javascript      更新时间:2023-09-26

我有一个对象数组,格式如下:

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

您可以这样做;

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