例如:
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);