【问题标题】:How can I convert each of the words in a string to each be an array?如何将字符串中的每个单词转换为数组?
【发布时间】:2021-07-02 07:46:56
【问题描述】:

有没有办法将字符串的每个单词转换为每个单词都有一个数组?例如,假设我有下面的变量。

Let str = "Just typing out some words"; 我想将其转换为下面的内容

Let arrs = [[Just], [typing], [out], [some], [words]]

有什么方法可以实现吗?

【问题讨论】:

  • 欢迎来到 Stack Overflow!请使用tour(您将获得徽章!)并通读help center,尤其是How do I ask a good question? 您最好的选择是进行研究,search 以获取有关 SO 的相关主题,然后试一试. 如果您在进行更多研究和搜索后遇到困难并且无法摆脱困境,请发布您的尝试minimal reproducible example,并具体说明您遇到的问题。人们会很乐意提供帮助。 (不是我的反对票)
  • 是的,这是可能的。分享您为获得更好响应所做的尝试。
  • 当然有办法实现这一点,但请向我们展示您迄今为止为解决问题所做的工作。

标签: javascript arrays string


【解决方案1】:

const str = "Just typing out some words";
const arr = str.split(' ').map((node) => [node]);
console.log(arr);

【讨论】:

  • @Aj4short 欢迎您。如果这解决了您的要求,请批准答案。
【解决方案2】:

这样就可以了。

str.split(" ").map((s) => [s])

【讨论】:

    猜你喜欢
    • 2013-11-27
    • 2016-02-04
    • 2016-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-08
    相关资源
    最近更新 更多