如何将单词数组拆分为数组数组,其中每个内部数组在 Javascript 中都是拆分字符串?



例如:

const arr = ['ab', 'cdef', 'ghi']
const split = [['a', 'b'], ['c', 'd', 'e', 'f'], ['g', 'h', 'i']]

如何在Javascript中做到这一点?我有点挣扎。如果有人能帮忙,我将不胜感激。

您可以使用.map()Spread Syntax

const data = ['ab', 'cdef', 'ghi'];
const result = data.map(s => [...s]);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

映射项目并使用空字符串拆分它们。

const arr = ['ab', 'cdef', 'ghi']
let result = arr.map(i => i.split(''))
console.log(result)

只需使用Array.map()

const arr = ['ab', 'cdef', 'ghi']
const result = arr.map((a)=>a.split(""));
console.log(result);

最新更新