1

I am having array in following format

"categories": [
  {
     "type": "A",               
     "subtype": [
        "X",
        "Y",
        "Z",
        "D",
         "E"           
     ],

  },
  {
     "type": "B",
     "Subtypes": [
        "0",
        "1",
        "2",
        "3",
        "4",
        "5"
     ],        
  },
  {
     "type": "C",         
     "includeConnectionTypes": [
        "@",
        "#",
        "$"
     ],
}]

I am having 2nd array array B

B = ["C","A"]

now how to filter elements in category array based on elements in array B

Mouser
  • 13,132
  • 3
  • 28
  • 54
  • A[t].type===B[i]?....... this is your needed? – Álvaro Touzón Oct 26 '17 at 10:11
  • 1
    you can read about [filter function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) – Toraaa Oct 26 '17 at 10:13
  • is there any lodash function to do the same? –  Oct 26 '17 at 10:14
  • 1
    @POM ... as for *lodash* based solutions see Kai's [answer](https://stackoverflow.com/questions/46951620/how-to-filter-collection-by-array-elements/#46951756) and the [2nd approach of mine](https://stackoverflow.com/questions/46951620/how-to-filter-collection-by-array-elements/#46952343). – Peter Seliger Oct 26 '17 at 12:53
  • @POM I just update my answer for more correct ;)! – Kai Oct 27 '17 at 06:22

4 Answers4

1

ES5

var result = categories.filter(function (item) {
    return B.indexOf(item.type) > -1;    
});

ES6

var result = categories.filter(item => B.indexOf(item.type) > -1);

This statement will check each element of "categories" array, if its type is an element of array B, it will be pushed to "result" array.

"indexOf" method returns the index of an element in an array, and if that array does not contain that element, this method will return -1.

Reference: Array.prototype.filter()

Văn Quyết
  • 2,384
  • 14
  • 29
1
var categories=[
  {
     "type": "A",               
     "subtype": [
        "X",
        "Y",
        "Z",
        "D",
         "E"           
     ],

  },
  {
     "type": "B",
     "Subtypes": [
        "0",
        "1",
        "2",
        "3",
        "4",
        "5"
     ],        
  },
  {
     "type": "C",         
     "includeConnectionTypes": [
        "@",
        "#",
        "$"
     ],
}];

It is a array,Now B is also array

var B = ["C","A"]

 var result=categories.filter(function(d){
    return B.indexOf(d.type)!=-1;
 });

"result" contains your expected result.

0

I think this is what you need: _.filter.

_.filter(categories, (item => B.indexOf(item.type)>= 0));
Kai
  • 3,104
  • 2
  • 19
  • 30
0

A combination of Array.prototype.filter and the bound usage of Array.prototype.some should make a well readable approach that also has the advantage of code-reuse via a sole specific function like doesCategoryTypeMatchAnyBoundType that, unlike the other approaches/solutions which do combine filter and indexOf, does not need to "know" the reference of the filter type list (there B from the given example).

// Q: I am having array in following format ...

var categories = [{
  "type": "A",
  "subtype": ["X", "Y", "Z", "D", "E"]
}, {
  "type": "B",
  "Subtypes": ["0", "1", "2", "3", "4", "5"]
}, {
  "type": "C",
  "includeConnectionTypes": ["@", "#", "$"]
}];

// ... I am having 2nd array array ...

var typeList = ["C","A"];

// ... now how to filter elements in category array based on
// elements in ... `typeList` ... array ...


// A: a combination of `Array.prototype.filter` and
// the bound usage of `Array.prototype.some` should make
// a well readable approach ...

function doesCategoryTypeMatchAnyBoundType(categoryItem) {
  return this.some(function (type) { return (categoryItem.type === type); });
}

var filteredCategoryList = categories.filter(doesCategoryTypeMatchAnyBoundType, typeList);

console.log("filteredCategoryList : ", filteredCategoryList);
.as-console-wrapper { max-height: 100%!important; top: 0; }

is there any lodash function to do the same?

Try sticking with the language core as long as you can. But if you are forced to use lodash, the just provided approach would change to ...

var categories = [{
  "type": "A",
  "subtype": ["X", "Y", "Z", "D", "E"]
}, {
  "type": "B",
  "Subtypes": ["0", "1", "2", "3", "4", "5"]
}, {
  "type": "C",
  "includeConnectionTypes": ["@", "#", "$"]
}];

var typeList = ["C","A"];


function doesCategoryTypeMatchAnyBoundType(categoryItem) {
  return _.some(this, function (type) { return (categoryItem.type === type); });
}

var filteredCategoryList = _.filter(categories, doesCategoryTypeMatchAnyBoundType.bind(typeList));

console.log("filteredCategoryList : ", filteredCategoryList);
.as-console-wrapper { max-height: 100%!important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
Peter Seliger
  • 11,747
  • 3
  • 28
  • 37