javascript - 分割并展平逗号分隔字符串的数组

标签 javascript node.js arrays string

最好的转换方式是什么:

const a = ["jan,feb,may", "apr,may,sept,oct", "nov,jan,mar", "dec", "oct,feb,jan"]

const res = ["jan","feb","may","apr","may","sept","oct","nov","jan","mar","dec","oct","feb","jan"]

PS:在javascript中,我尝试使用array.reduce()和分割方法。但这没有用。这是我尝试过的

let flat = arr.reduce((arr, item) => [...arr, ...item.split(',')]);

最佳答案

使用Array#flatMapString#split :

const a = ["jan,feb,may", "apr,may,sept,oct", "nov,jan,mar", "dec", "oct,feb,jan"];

const res = a.flatMap(e => e.split(','));

console.log(res);

关于javascript - 分割并展平逗号分隔字符串的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69963007/

相关文章:

javascript - 从 Mongoose 结果匿名函数返回父函数

node.js - 如何在回调之外使用mongoclient访问 Node 中的mongodb

java - 摩尔斯电码转换器

javascript - 如何在没有内置函数的情况下反转字符串 javascript ..?

java - 字符串数组在 for 循环中初始化时忽略索引 0

javascript - 如何简单地编码和解码字符串变量

javascript - Dreamweaver 窗体按钮

javascript - React.js,ES6,如何访问ES6类中的react-router Lifecycle?

javascript - 使用异步函数中的数据

node.js - 在 CNAME 重定向之前拦截流量