最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

arrays - How to filter multiple values (OR operation) in javascript - Stack Overflow

programmeradmin3浏览0评论

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

I need to filter multiple values extracted from another array, so OR operator is not my way because the amount of values 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 hard to me to apply it to my problem.

And this is for Angularjs.

I hope you can give me a hand.

P.S : Sorry about my english, its not my native language.

regards

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

I need to filter multiple values extracted from another array, so OR operator is not my way because the amount of values 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 hard to me to apply it to my problem.

And this is for Angularjs.

I hope you can give me a hand.

P.S : Sorry about my english, its not my native language.

regards

Share Improve this question edited May 23, 2017 at 12:10 CommunityBot 11 silver badge asked Jan 10, 2016 at 21:58 llermalyllermaly 2,5103 gold badges19 silver badges35 bronze badges 13
  • ok=["190", "200"]; data.filter(/./.test, RegExp("^("+ok.join(")|(")+")")); – dandavis Commented Jan 10, 2016 at 22:07
  • 1 arr.filter(function(item){return ["190","200"].indexOf(item[0])>=0}) – CupawnTae Commented Jan 10, 2016 at 22:08
  • 1 @dandavis why? You mean >-1 instead of >=0 maybe? – CupawnTae Commented Jan 10, 2016 at 22:10
  • 2 @CupawnTae, why don't you write it as an answer? – Shomz Commented Jan 10, 2016 at 22:14
  • 1 @Shomz thanks, done. I actually only sat down to fish a splinter out of my finger, back to work now :-) – CupawnTae Commented Jan 10, 2016 at 22:20
 |  Show 8 more ments

2 Answers 2

Reset to default 10

You can use indexOf() on an array to find if the array contains a value. It will be -1 if not present, otherwise it will be the index in the array of the first instance of the value, i.e. >=0

So, for example:

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

As per your ment, the filters keys are embedded inside nested arrays. You can extract these using map(), e.g.

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

A function implementing this (thanks to @Shomz for the bulk of the code) would look like:

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

const pare_set = ["190", "200", "201"] // here we can set the numbers we want to use to filter the rows, only for educational purposes, i suppose you will extract them from another place.

const result = arr.filter(o => pare_set.includes(o[0]))

Array.includes looks for a value inside the Array, if exists returns true, and filter expects a true. if not is discarded.

发布评论

评论列表(0)

  1. 暂无评论