在Javascript中映射数组数组的每个数组的第一个元素 [英] Map the first element of each array of an array of arrays in Javascript

查看:71
本文介绍了在Javascript中映射数组数组的每个数组的第一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的数组数组:

I have an array of arrays like this:

myData = [
          ["name1", 34.1, 43.1, 55.2],
          ["name2", 5.3, 23.6, 40.9],
          ["name3", 43.5, 77.8, 22.4]
         ];

我想得到一个只包含每个数组的第一个元素的数组,如下所示: [name1,name2,name3]

I want to get an array containing only the first element of each array like this: ["name1", "name2", "name3"].

我试图这样做,但不起作用:

I tried to do it like this but doesn't work:

var arrayTitle = myData.map(function(x) {
    return [myData[x][0]];
});

有什么建议吗?

推荐答案

你可以只返回 x 的第一个elementn,它是外部数组的一个元素。

You could return just the first elementn of x, an element of the outer array.

var myData = [["name1", 34.1, 43.1, 55.2], ["name2", 5.3, 23.6, 40.9], ["name3", 43.5, 77.8, 22.4]],
    arrayTitle = myData.map(function(x) {
        return x[0];
    });

console.log(arrayTitle);

这篇关于在Javascript中映射数组数组的每个数组的第一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆