简体   繁体   English

从多维数组收集数据?

[英]Collecting data from a Multi-Dimensional Array?

If I had something like this: 如果我有这样的事情:

var multi_array = [[1,1], [2,2]];

how could i pull data from the second array in the multi-array and maybe log it to the console. 我如何才能从多阵列中的第二个阵列中提取数据并将其记录到控制台。

Well, you couldn't since you effectively have a syntax error. 嗯,您不能,因为您实际上遇到了语法错误。 You can't use - in a variable name. 您不能在变量名中使用- But looking past that... 但是过去...

var multi_array = [[1,1], [2,2]];
console.log(multi_array[1]); // Outputs:  [2,2]

The first array is at multi_array[0] , and the second at multi_array[1] . 第一个数组位于multi_array[0] ,第二个数组位于multi_array[1] Suppose you wanted to get the second element in the second array: 假设您想获取第二个数组中的第二个元素:

console.log(multi_array[1][1]);

I guess you don't really mean the second array, but the second value of the inner array. 我猜你不是真正的意思第二个数组,而是内部数组的第二个值。 Use map to pick only special values from the inner arrays: 使用map从内部数组中仅选择特殊值:

var ar = [[1, 2], [3, 4], [5, 6]];

var new_ar = ar.map(function(inner_ar){
   return inner_ar[1];
});

console.log(new_ar); // [2, 4, 6]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM