Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

 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);
                 });
like image 505
badigard Avatar asked Jun 26 '16 14:06

badigard


1 Answers

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));
like image 195
Aditya Singh Avatar answered Sep 18 '22 02:09

Aditya Singh