检索二维数组的选定列

时间:2019-06-09 10:33:11

标签: javascript arrays multidimensional-array

我需要检索2维数组的选定列,我有2维数组

var mytext = "mytextbox";
            var json =
                new
                {
                    MemberName = mytext,
                    MemeberEmail = "mack @mack.com",
                    MemberPassword = "111"
                };
            return JsonConvert.SerializeObject(json);

预期是

var indexes = [2,3,5];
var data = [
["Head1","Head2","Head3","Head4","Head5", "Head6"],
[10,11,12,13,14,15,16],
[20,21,22,23,24,25,26],
[30,31,32,33,34,35,36],
];

如何获取结果数组,同时必须能够动态更改位置的索引数组。

我试图删除第一个标题

var result = [
[13,14,16],
[23,24,26],
[33,34,36],
]

1 个答案:

答案 0 :(得分:2)

使用正确的索引,您可以映射值。

ob10
var indices = [2, 3, 5],
    data = [["Head1", "Head2", "Head3", "Head4", "Head5", "Head6"], [10, 11, 12, 13, 14, 15, 16], [20, 21, 22, 23, 24, 25, 26], [30, 31, 32, 33, 34, 35, 36]],
    result = data.slice(1).map(a => indices.map(i => a[i]));

console.log(result);

ES5

.as-console-wrapper { max-height: 100% !important; top: 0; }
var indices = [2, 3, 5],
    data = [["Head1", "Head2", "Head3", "Head4", "Head5", "Head6"], [10, 11, 12, 13, 14, 15, 16], [20, 21, 22, 23, 24, 25, 26], [30, 31, 32, 33, 34, 35, 36]],
    result = data.slice(1).map(function (a) {
        return indices.map(function (i) {
            return a[i];
        });
    });

console.log(result);