Javascript Nested Filters in Array 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 that consists of objects from a full array:

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

I would like to filter objects from the full list, where in the id array of the object's object at least one of the selectedID values ​​appears.

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

and then add them to the tmp list.

I tried to use filters, but could not fully understand this.

Thank.

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);
                 });
+4
source share
7 answers

First of all, this line in your question is incorrect

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

You cannot declare arrays with {}. Use instead[]

, # # ,

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));
Hide result
+3

- .

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

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);
Hide result
+5

:

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);
Hide result
+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'
  }]
}];

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)
Hide result
0
array1
array2

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

array1 ... .

0

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);
Hide result
-1

1. market

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

-2

All Articles