How to filter multiple values ​​(OR operation) in javascript

I have an array of arrays and I need to filter by position 0 elements.

I need to filter out several values ​​extracted from another array, so the OR operator is not my path, because the number of values ​​that I have is not fixed.

var arr = [
    ["202",27,44],
    ["202",194,35],
    ["200",233,344],
    ["190",333,444],
];

var newArr = arr.filter(function(item){
    //fixed amount of values, not working for my purpose
    return item[0] === "190" || item = "200"
});

I need something like

var newArr = arr.filter(function(item){    
    return item[0] === filterValues
    //where filterValues = ["190","200",etc]
});
Function

in this case should return:

[["200",233,344],["190",333,444]]

This question uses underscorejs, but im new to javascript, so it was difficult for me to apply it to my problem.

And this is for Angularjs.

I hope you can lend a hand to me.

PS: Sorry for my English, this is not my native language.

considers

+4
source share
1

indexOf() , , . -1 , , .. >=0

, :

arr.filter(function(item){return ["190","200"].indexOf(item[0])>=0})

. map(),

var keys = [["190",222],["200",344]].map(function(inner){return inner[0]});

, ( @Shomz ) :

var arr = [
    ["202",27,44],
    ["202",194,35],
    ["200",233,344],
    ["190",333,444],
];
  
function myFilter(query) {
  return arr.filter(function(item){
    return query.indexOf(item[0]) >= 0;
  })
}

var q = [["190",222],["200",344]];
var keys = q.map(function(inner){
  return inner[0];
});

alert(myFilter(keys));
+10

Source: https://habr.com/ru/post/1623677/


All Articles