数组Array中的Javascript嵌套过滤器

时间:2016-06-26 14:08:22

标签: javascript arrays filter

我有这种格式的对象数组:

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

我想过滤掉完整列表中的对象,其中至少有一个selectedIDs值出现在对象的项目的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);
                 });

7 个答案:

答案 0 :(得分:5)

您可以对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);




答案 1 :(得分:3)

首先,你问题中的这一行是错误的

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

您无法使用{}声明数组。而是使用[]

对于您的问题,您可以使用数组#过滤器数组#一些方法使用纯功能方法来获得您想要的结果,如下所示:

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

答案 2 :(得分:2)

你可以这样做;



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




答案 3 :(得分:0)

我认为这可能是一个解决方案。



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)




答案 4 :(得分:0)

array1
array2

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

将使用array1并针对第二个值对每个值运行它。

答案 5 :(得分:-1)

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

答案 6 :(得分:-2)

看着你的工作 1.没有像market

那样的东西

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