Javascript nested filters in Array of arrays

▼魔方 西西 提交于 2021-02-18 06:42:50

问题


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:

 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

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

and then add them to the tmp list.

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

回答1:


First of all, this line in your question is wrong

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:

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:


You could use a hash table for the selectedID and use it for fast filtering.

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



回答3:


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



回答4:


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.




回答5:


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)



回答6:


Looking at your job 1. There is nothing like market

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




回答7:


Spread operator should simplify some code:

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


来源:https://stackoverflow.com/questions/38039311/javascript-nested-filters-in-array-of-arrays

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!