嘿,我在javascript中需要一些帮助
[ [ 0, 0, 0, -8.5, 28, 8.5 ],
[ 1, 1, -3, 0, 3, 12 ],
[ 2, 2, -0.5, 0, 0.5, 5.333333333333333 ] ]
我希望上面的数组采用此
的形式 0 0 0 -8.5 28 8.5, 1 1 -3 0 3 12, 2 2 -0.5 0 0.5 5.333333333333333
concat和reduce在每个值之后放置一个逗号
答案 0 :(得分:3)
您只需使用Array.prototype.map()& Array.prototype.join()
<强> 实施例 强>
var myArr = [ [ 0, 0, 0, -8.5, 28, 8.5 ],
[ 1, 1, -3, 0, 3, 12 ],
[ 2, 2, -0.5, 0, 0.5, 5.333333333333333 ] ];
var str = myArr.map(insideArr => insideArr.join(" ")).join();
console.log(str);
答案 1 :(得分:2)
arr.map(item => item.join(' ')).join(',');
答案 2 :(得分:0)
这个问题可以分两步回答。首先,使用带有扩展运算符(...)的Array.prototype.reduce()高阶函数展平数组。然后,使用Array.prototype.join()方法将展平的数组转换为数字列表。
const arr = [ [ 0, 0, 0, -8.5, 28, 8.5 ],
[ 1, 1, -3, 0, 3, 12 ],
[ 2, 2, -0.5, 0, 0.5, 5.333333333333333 ] ];
const flatten = arr.reduce((combine, item) => [...combine, ...item], [])
.join(' ');