gpt4 book ai didi

javascript - 在 2d Array Javascript 中查找零索引数据

转载 作者:行者123 更新时间:2023-11-28 12:13:34 26 4
gpt4 key购买 nike

获取二维数组javascript中所有0索引数据

let a = [["", "2", "", ""], ["1", "3", "", ""], ["", "", "4", ""]]
index = 0

output = ["", "1", ""]

同样获取所有剩余索引数据

index= 1
output = ["2", "3", ""]

最佳答案

您可以使用Array#map方法 ES6 destructuring feature 。解构有助于从对象中提取某些属性(Javascript Array也是一个对象)。

let a = [
["", "2", "", ""],
["1", "3", "", ""],
["", "", "4", ""]
];
let index = 0;

let output = a.map(({[ index ]: v }) => v)

console.log(output);


index = 1;

output = a.map(({[ index ]: v }) => v)

console.log(output);

关于javascript - 在 2d Array Javascript 中查找零索引数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55087344/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com