gpt4 book ai didi

javascript - 将字符串数组转换为 JAVASCRIPT 对象

转载 作者:行者123 更新时间:2023-11-30 09:11:27 25 4
gpt4 key购买 nike

我有问题,我有一个字符串数组

[
"Time:25/10/2019 14:49:47.41,Server:Daniel.Europe.A…itical,Area:Europe,Site:,Station:Aberdeen,Stream:",
"Time:25/10/2019 14:49:48.16,Server:Daniel.Europe.U…,Area:Europe,Site:United Kingdom,Station:,Stream:"
]

我需要将它转换为对象

[
{"Time" : "25/10/2019 14:49:47.41", "Server", "Daniel.Europe..", .. },
{}
]

同样。

JSON.parse 不适用于非序列化字符串。

最佳答案

使用 Object.fromEntries()

var data = [
"Time:25/10/2019 14:49:47.41,Server:Daniel.Europe.A…itical,Area:Europe,Site:,Station:Aberdeen,Stream:",
"Time:25/10/2019 14:49:48.16,Server:Daniel.Europe.U…,Area:Europe,Site:United Kingdom,Station:,Stream:"
]

var result = data.map(v =>
Object.fromEntries(v.split(',').map(v => v.split(/:(.*)/)))
)

console.log(result)

关于javascript - 将字符串数组转换为 JAVASCRIPT 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58626246/

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