gpt4 book ai didi

Javascript按行分隔字符串插入数组

转载 作者:行者123 更新时间:2023-12-02 22:49:41 26 4
gpt4 key购买 nike

我有一个多行字符串。

one
two
three

我需要将它们添加到以行分隔的数组中

我怎样才能做到这一点?

最佳答案

您可以使用string.split在换行符处剪切字符串。您可以添加 Array.filter 来删除空行。

Filter 将在每个 string 处循环并创建一个新数组。如果字符串为空,则不会将其推送到新数组。

const str = `one
two

three`;

const ret = str.split('\n').filter(x => x.length);

console.log(str);
console.log(ret);

关于Javascript按行分隔字符串插入数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58233689/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com