【问题标题】:How to split an array of words into an array of arrays where each inner array is a split string in Javascript? [duplicate]如何将单词数组拆分为数组数组,其中每个内部数组都是Javascript中的拆分字符串? [复制]
【发布时间】:2025-12-16 15:40:01
【问题描述】:

例如:

const arr = ['ab', 'cdef', 'ghi']

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

如何在 Javascript 中做到这一点?我有点挣扎。如果有人可以提供帮助,我将不胜感激。

【问题讨论】:

  • 映射和分割...
  • 你试过什么?请出示您的代码。

标签: javascript arrays sub-array


【解决方案1】:

您可以使用.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; }

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      只需使用Array.map()

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

      【讨论】: