JavaScript的比较数组 [英] JavaScript compare arrays
问题描述
我在以下格式的数组:
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],
];
然后我有被传递到页面(M = 1,2),这被逗号分隔的查询字符串,然后分割创建类似如下的数组:
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']
我需要做的是找到所有的'标志',其中ID(标记[I] [4])来自查询字符串。
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.
任何帮助将大大AP preciated。
Any help would be greatly appreciated.
感谢
推荐答案
一种选择是使用嵌套循环:
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/
这篇关于JavaScript的比较数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!