我在javascript中具有以下数组:
DATA[
[ 3, "Name1" ],
[ 5, "Name2" ],
[ 10, "Name3" ],
[ 55, "Name4" ]
]
我想将其转换为:
DATA[ "Name1", "Name2", "Name3", "Name4" ]
可以在javascript中使用吗?
谢谢
答案 0 :(得分:1)
有几种方法可以做到这一点,但这是一种:
for(var i=0;i<DATA.length;i++){
DATA[i] = DATA[i][1];//only grab the 2nd item in the array
}
答案 1 :(得分:1)
Array.prototype.map
的简单用例
let data = [
[3, "Name1"],
[5, "Name2"],
[10, "Name3"],
[55, "Name4"]
];
data = data.map(arr => arr[1]);
console.log(data);
答案 2 :(得分:0)
只需使用map并获取每个子数组的第二项即可构建新数组。
const DATA = [
[ 3, "Name1" ],
[ 5, "Name2" ],
[ 10, "Name3" ],
[ 55, "Name4" ]
]
const newArr = DATA.map(i => i[1])
console.log(newArr)
答案 3 :(得分:0)
尝试一下:
let data = [
[3, "Name1"],
[5, "Name2"],
[10, "Name3"],
[55, "Name4"]
];
const result = data.map(([k, v])=>v);
console.log(result);
答案 4 :(得分:0)
尝试一下:
Year 2017 2018 2019 All Mean Change Change_mean
Branch
BILING 1406 1280 4 2690.0 897.0 -100.0 -100.0
BILWPL 199 117 239 555.0 185.0 104.0 29.0
BKCLUB 94 161 238 493.0 164.0 48.0 45.0