5

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

13
  • ok=["190", "200"]; data.filter(/./.test, RegExp("^("+ok.join(")|(")+")")); Commented Jan 10, 2016 at 22:07
  • 1
    arr.filter(function(item){return ["190","200"].indexOf(item[0])>=0}) Commented Jan 10, 2016 at 22:08
  • 1
    @dandavis why? You mean >-1 instead of >=0 maybe? Commented Jan 10, 2016 at 22:10
  • 2
    @CupawnTae, why don't you write it as an answer? 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 :-) Commented Jan 10, 2016 at 22:20

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

5 Comments

Added a quick full example so OP can see it in action here.
Thanks! , tested in CasperJS and working. I tried with 'include()' answer and didnt work . I dont know if it was my fault or PhantomJS 1.9.2 is not supporting this method yet
just noticed that my "query" array is array of arrays too .. tried with var q = [["190",222],["200",344]]; return query[0].indexOf(item[0]) >= 0; but just returned ["190",333,44] :(
@llermaly yeah, that won't work - one solution would be to map the inner arrays to their first element, e.g. var q = [["190",222],["200",344]].map(function(inner){return inner[0]});
Thanks ! I have so much to learn , but I'm happy theres people like you around there, I hope to be answering soon :) . Have a nice day
3
const compare_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 => compare_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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.