本文介绍了如何从Javascript中的数组数组中提取值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个变量,如下所示:
I have a variable as follows:
var dataset = {
"towns": [
["Aladağ", "Adana", [35.4,37.5], [0]],
["Ceyhan", "Adana", [35.8,37], [0]],
["Feke", "Adana", [35.9,37.8], [0]]
]
};
该变量中包含许多城镇数据.如何有效地从数据中提取第三个元素的第一个元素?即,...
将在下面是什么?
The variable has a lot of town data in it. How can I extract the first elements of the third ones from the data efficiently? I,e, what will ...
be below?
var myArray = ...
//myArray == [35.4,35.8,35.9] for the given data
如果我要将两个值都存储在数组中怎么办?那是
And what to do if I want to store both values in the array? That is
var myArray = ...
//myArray == [[35.4,37.5], [35.8,37], [35.9,37.8]] for the given data
我是Java的新手.我希望有一种不用for循环的方法.
I'm very new to Javascript. I hope there's a way without using for loops.
推荐答案
在较新的浏览器上,可以使用map
或forEach
来避免使用for
循环.
On newer browsers, you can use map
, or forEach
which would avoid using a for
loop.
var myArray = dataset.towns.map(function(town){
return town[2];
});
// myArray == [[35.4,37.5], [35.8,37], [35.9,37.8]]
但是for循环更兼容.
But for loops are more compatible.
var myArray = [];
for(var i = 0, len = dataset.towns.length; i < len; i++){
myArray.push(dataset.towns[i][2];
}
这篇关于如何从Javascript中的数组数组中提取值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!