我有一个这样的数组:
var array=[
['0000'],
['0000']
]
我想得到这样的结果:
var result=[
[0,0,0,0],
[0,0,0,0]
]
javascript map 这怎么可能?
我的带有愚蠢 for 循环的解决方案如下所示:
var array=[
['0000'],
['0000']
]
var new_array=[]
for (var i=0; i<array.length;i++) {
new_array.push((array[i].toString().split('').map(Number)))
}
console.log(new_array)
Edit 1.0 How to get the same result if my array looks like this:
['0000','0000','0000']
Edit 2.0 Ok last edit - How to turn around the process? So getting from
[[0,0,0,0], [0,0,0,0]]
-- >[['0000'], ['0000']]
EDIT 3 -- OK found the solution for Edit 2 on my own:
array.map(a => [a.join('')])
感谢您的帮助。
乔纳斯
最佳答案
To handle the following scenario...
var arrays=[
['0000', '0000', '0000'],
['0000', '0000', '0000'],
['0000', '0000', '0000'],
// etc...
];
arrays.map(ary => ary.reduce((ary, stg) => ary.concat(stg.split('').map(Number)), []));
// => [[0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0, etc...], etc...];
A little shorter based on answer from @Josh from Qaribou
arrays.map(ary => ary.reduce((ary, stg) => ary.concat(Array.from(stg, Number)), []));
关于javascript - 使用 map 将二维字符串数组转换为拆分数值数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47228211/