我正在尝试将数组的每个数组转换为字符串。
我知道方法 flat,其中数组的所有数组变成单个数组,但这并不能解决我的全部目的。
array = [['ba','ab'],['bab','abb']]
我尝试过的代码是:
let merged = array.map(a => {
console.log(a)
a.reduce((a, b) => a.concat(b), []);
})
console.log(merged);
预期输出为:[['ba,ab'],['bab, abb']]
最佳答案
您可以使用Array.prototype.join()
以此目的。来自文档:
The
join()
method creates and returns a new string by concatenating all of the elements in an array (or an array-like object), separated by commas or a specified separator string. If the array has only one item, then that item will be returned without using the separator.
就像下面这样:
const array = [['ba', 'ab'], ['bab', 'abb']];
const result = array.map(e => e.join(','));
console.log(result);
希望有帮助!
关于javascript - 将数组的数组的每个数组转换为字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59653325/