最好的转换方式是什么:
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#flatMap
和 String#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/