0

I have an array in the following format:

var markers = [
    ['Title', 15.102253, 38.0505243, 'Description', 1], 
    ['Another Title', 15.102253, 38.0505243, 'Another Description', 2],
    ['Title 3', 15.102253, 38.0505243, 'Description 3', 3], 
];

I then have a query string being passed to the page (m=1,2), which being comma separated, is then split to create an array like the following:

['1', '2']

What I need to do is find all 'markers' where the ID (markers[i][4]) comes from the query string.

What would be the best way to achieve this? Ideally I want to create a 3rd array in the same format as 'markers' but only showing the results from the query string.

Any help would be greatly appreciated.

Thanks

digitalclubb
  • 585
  • 1
  • 5
  • 14

2 Answers2

2

One option is to use nested loops:

var markers = [
    ['Title', 15.102253, 38.0505243, 'Description', 1], 
    ['Another Title', 15.102253, 38.0505243, 'Another Description', 2],
    ['Title 3', 15.102253, 38.0505243, 'Description 3', 3], 
];
var search = ['1', '2'];
var result = [];

for (var i = 0; i < search.length; i++)
    for (var j = 0; j < markers.length; j++)
        if (search[i] == markers[j][4]) {
            result.push(markers[j]);
            break;
        }

console.log(result);

DEMO: http://jsfiddle.net/3TErD/

VisioN
  • 143,310
  • 32
  • 282
  • 281
2

Couldn't you just use a nested loop here?

var filteredMarkers = [];

for(var i = 0; i < markers.length; i++) {

    for(var j = 0; j < queryStringArray.length; j++) {

        // this might need to be changed as your markers has index 4 as a number whereas the queryString array appears to be strings.
        if(markers[i][4] === queryStringArray[j]) {

            filteredMarkers.push(markers[i]);
            break;

        }

    }

}
VisioN
  • 143,310
  • 32
  • 282
  • 281
dougajmcdonald
  • 19,231
  • 12
  • 56
  • 89