JavaScript将字符串转换为数组

huangapple go评论89阅读模式
英文:

Javascript convert string into array

问题

我有一个以下格式的字符串,如何将此字符串转换为数组的数组,以便我可以轻松访问其中的项目?

data = '[[Bday Party , 2023-05-16], [Bday Party , 2023-05-16], [Bday Party , 2023-05-16], [Bday Party , 2023-05-16], [Bday Party , 2023-05-16], [Bday Party , 2023-05-16]]'

我尝试过使用 Array.from(data),但它没有起作用!

英文:

I have a string of the following format, how do I convert this string into an array of arrays so I can easily access the items?

data = '[[Bday Party , 2023-05-16], [Bday Party , 2023-05-16], [Bday Party , 2023-05-16], [Bday Party , 2023-05-16], [Bday Party , 2023-05-16], [Bday Party , 2023-05-16]]'

I have tried Array.from(data) but it didn't work!

TIA

答案1

得分: 1

不是完美的,但应该能完成工作:

<!-- 开始片段:js 隐藏:false 控制台:true babel:false -->

<!-- 语言:lang-js -->

    const data = '[[生日聚会,2023-05-16],[生日聚会,2023-05-16],[生日聚会,2023-05-16],[生日聚会,2023-05-16],[生日聚会,2023-05-16],[生日聚会,2023-05-16]]';

    const json = JSON.parse(data.replace(/\s*([^[\],]+?)\s*(?=[,\]])/g,'"$1"'));

    console.log(json);

<!-- 结束片段 -->

## 正则表达式的工作原理:

\s*([^[],]+?)\s*(?=[,]])


[在这里](https://regex101.com/r/cFMpVf/1) 您可以看到它的工作原理并尝试一下

| 语法 | 解释 |
| - | - |
| `\s*` | 任何空白字符,用于去除每个条目的前导空格 |
| `(` | 第一个组开始,用于捕获每个条目,例如 `生日聚会` |
| `[^[\],]+?` | 不包含 `[`,`]` 和 `,` 的一个或多个字符。`+?` 表示一个或多个但不贪婪,这是为了避免捕获尾随空格(由于某些条目在中间包含空格,我们不能排除字符集中的空格) |
| `)` | 第一个组结束,到目前为止,我们在第一个组中捕获了所需的条目,例如 `生日聚会` |
| `\s*` | 条目后的任何尾随空格,我们不需要在捕获组中 |
| `(?=[,\]])` | 一个前瞻,我们匹配的所有内容后面必须跟着 `,`(列表分隔符)或 `]`(数组的结尾) |
英文:

It's not perfect but should get the jobs done:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const data = &#39;[[Bday Party , 2023-05-16], [Bday Party , 2023-05-16], [Bday Party , 2023-05-16], [Bday Party , 2023-05-16], [Bday Party , 2023-05-16], [Bday Party , 2023-05-16]]&#39;;

const json = JSON.parse(data.replace(/\s*([^[\],]+?)\s*(?=[,\]])/g, &#39;&quot;$1&quot;&#39;));

console.log(json);

<!-- end snippet -->

How the regex works:

\s*([^[\],]+?)\s*(?=[,\]])

Here you can see how it works and play around a little bit

syntax explain
\s* any white spaces, this is for getting rid of the leading spaces from each entry
( group one start, this is for capturing each entry, such as Bday Party
[^[\],]+? 1 or more characters that does not contain [, ] and ,. +? means 1 or more but non-greedy, this is for avoiding capturing trailing spaces (we can't rule out white spaces inside of the character set, since some of the entries contains white spaces in the middle)
) group one end, so far we have captured the entry we need in group one, such as Bday Party
\s* any trailing whitespaces after an entry, we don't need that in our capturing group
(?=[,\]]) a lookahead, everything we have matched must be followed by a ,(list separator) or ](end of an array)

答案2

得分: 1

你可以使用以下的字符串拆分方法:

var data = '[[生日派对 , 2023-05-16], [生日派对 , 2023-05-16], [生日派对 , 2023-05-16], [生日派对 , 2023-05-16], [生日派对 , 2023-05-16], [生日派对 , 2023-05-16]]';
data = data.substring(2, data.length - 2);
var array = data.match(/\[.*?\]/g);
var output = [];
for (var i=0; i < array.length; ++i) {
    output.push(array[i].substring(1, array[i].length - 1).split(/\s*,\s*/));
}
console.log(output);
英文:

You could use the following string splitting approach:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var data = &#39;[[Bday Party , 2023-05-16], [Bday Party , 2023-05-16], [Bday Party , 2023-05-16], [Bday Party , 2023-05-16], [Bday Party , 2023-05-16], [Bday Party , 2023-05-16]]&#39;;
data = data.substring(2, data.length - 2);
var array = data.match(/\[.*?\]/g);
var output = [];
for (var i=0; i &lt; array.length; ++i) {
    output.push(array[i].substring(1, array[i].length - 1).split(/\s*,\s*/));
}
console.log(output);

<!-- end snippet -->

答案3

得分: 0

许多关于正则表达式的好建议,但如果你觉得直接字符串操作更容易,这里有一个例子:

var rawData = "[[Bday Party , 2023-05-16], [Bday Party , 2023-05-16], [Bday Party , 2023-05-16], [Bday Party , 2023-05-16], [Bday Party , 2023-05-16], [Bday Party , 2023-05-16]]";

var rows = rawData.slice(2, rawData.length - 2).split('], [');
var data = rows.map(x => x.split(", "));

console.log(data);

与所有答案一样,我们不知道你所接收的格式的定义,它不是标准格式,所以这段代码可能无法处理结构有变化的数据。

英文:

Many good suggestions involving regular expressions, but in case you find it easier with direct string manipulation, here's an example of that:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var rawData = &quot;[[Bday Party , 2023-05-16], [Bday Party , 2023-05-16], [Bday Party , 2023-05-16], [Bday Party , 2023-05-16], [Bday Party , 2023-05-16], [Bday Party , 2023-05-16]]&quot;;

var rows = rawData.slice(2, rawData.length - 2).split(&#39;], [&#39;);
var data = rows.map(x =&gt; x.split(&quot;, &quot;));

console.log(data);

<!-- end snippet -->

As with all answers, we don't know the definition of the format you're receiving, and it's not a standard one, so this snippet may not work on data with variations in structure.

huangapple
  • 本文由 发表于 2023年6月9日 14:46:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76437845.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定