javascript - 将数组的数组的每个数组转换为字符串数组

标签 javascript arrays

我正在尝试将数组的每个数组转换为字符串。

我知道方法 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/

相关文章:

javascript - 图像和 PDF 制作

ios - 如何在 switch 语句中使用谓词

c - 用随机数和字母初始化c中的2d矩阵

arrays - 删除所有具有特定索引的字段

javascript - 在滚动上交换我的 Logo 图像(修改现有脚本)

javascript - 在我的 Meteor 应用程序中哪里包含 Parse 初始化 key ?

javascript - 为什么事件没有通过?

JSON 响应的 JavaScript 百分比计算

javascript - 如何实例化并获取多维数组/矩阵的长度?

javascript - 如何在不重绘表的情况下更新数据表中的行?