1

I have an array that looks like this:

[['place', '1', '-2'],['place2', '1', '-2'],['place3', '1', '-3']]

I want to filter it using a keyword from onClick so it looks like this:

[['place', '1', '-2'],['place2', '1', '-2']]

If my filter is for example -2.

The code i'm using now is (where object is the variable being filtered):

return $.map(object, function (item, key) {
   if (item[0] === value) {
       return item;
   }
});

And then I call it using:

var markers = results(filter);

But the result I get is blank.

Hopefully I am making sense if not I am very sorry please let me know and I can try to clarify.

4
  • do you like to search in every element for -2? Commented Oct 22, 2016 at 20:32
  • If your value to be filtered appears anywhere in array you can also use index of on inner array in addition to answers given. But it might not work on older browsers. Commented Oct 22, 2016 at 20:34
  • @NinaScholz in this example yes Commented Oct 22, 2016 at 20:41
  • @ShaileshVaishampayan my goal is to have a filter for [1] [0] [2] Commented Oct 22, 2016 at 20:41

2 Answers 2

0

You could use Array#filter for it and return the filtered items.

var data = [['place', '1', '-2'], ['place2', '1', '-2'], ['place3', '1', '-3']],
    filtered = data.filter(function (a) {
        return a[2] === '-2';
    });

console.log(filtered);

Search for any item with the given string

var data = [['place', '1', '-2'], ['place2', '1', '-2'], ['place3', '1', '-3']],
    filtered = data.filter(function (a) {
        return a.some(function (b) {
            return b === '-2';
        });
    });

console.log(filtered);

ES6, Search for any item with the given string

var data = [['place', '1', '-2'], ['place2', '1', '-2'], ['place3', '1', '-3']],
    filtered = data.filter(a => a.some(b => b === '-2'));

console.log(filtered);

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

4 Comments

This could work I will give it a try thanks
This almost works. One issue I am having is return a[3] === 'text'; works while return a[3] === filterfromfunction; does not any idea?
please add the content of filterfromfunction.
I ended up making a global variable and that worked
0

Try this

    var places = [['place', '1', '-2'],['place2', '1', '-2'],['place3', '1', '-3']]
    var result = places.filter(place => place[2] === '-2')

    console.log(result) // [['place', '1', '-2'],['place2', '1', '-2']]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.