gpt4 book ai didi

javascript - 在 javascript 中将 Map 转换为 json

转载 作者:行者123 更新时间:2023-11-29 10:55:57 25 4
gpt4 key购买 nike

我有一个 Map<String, Object>我想将其转换为 JSON .

这是我所拥有的:
要点1:声明变量

var map = new Map(), //map
jsonObject= {}, // oject that needs to be converted to JSON
value1 = { topic: "someText1", status: "0", action: "1", comment: "someComment1" }, // values of Map
value2 = { topic: "someText2", status: "0", action: "0", comment: "someComment2" },
value3 = { topic: "someText3", status: "1", action: "1", comment: "someComment3" };

第 2 点:填充 map
//map的key是网页中各种属性的拼接,用|

分隔
map.set('release|attachment|license1|topic1', value1);
map.set('release|attachment|license1|topic2', value1);
map.set('release1|attachment1|license1|topic1', value1);
map.set('release1|attachment1|license2|topic2', value2);
map.set('release1|attachment1|license3|topic3', value3);
map.set('release2|attachment2|license2|topic2', value2);
map.set('release2|attachment2|license2|topic3', value2);

第 3 点: 迭代 map 并填充 jsonObject

for (const [key, values] of map) {
setPath(jsonObject, key.split('|'), values);
}
function setPath(obj, [...keys], item) {
keys.pop(); // removing topic
const last = keys.pop();
keys.reduce((r, a) => r[a] = r[a] || {}, obj)[last] = [item];
}

第 4 点电流输出 [ console.log(JSON.stringify(jsonObject)); ]

{
"release": {
"attachment": {
"license1": [
{
"topic": "someText2",
"status": "0",
"action": "0",
"comment": "someComment2"
}
]
}
},
"release1": {
"attachment1": {
"license1": [
{
"topic": "someText1",
"status": "0",
"action": "1",
"comment": "someComment1"
}
],
"license2": [
{
"topic": "someText2",
"status": "0",
"action": "0",
"comment": "someComment2"
}
],
"license3": [
{
"topic": "someText3",
"status": "1",
"action": "1",
"comment": "someComment3"
}
]
}
},
"release2": {
"attachment2": {
"license2": [
{
"topic": "someText3",
"status": "1",
"action": "1",
"comment": "someComment3"
}
]
}
}
}

第 5 点预期输出 (jsonObject)

{
"release": {
"attachment": {
"license1": [
{
"topic": "someText1", // ^^ This object is missing in current output.
"status": "0",
"action": "1",
"comment": "someComment1"
},
{
"topic": "someText2",
"status": "0",
"action": "0",
"comment": "someComment2"
}
]
}
},
"release1": {
"attachment1": {
"license1": [
{
"topic": "someText1",
"status": "0",
"action": "1",
"comment": "someComment1"
}
],
"license2": [
{
"topic": "someText2",
"status": "0",
"action": "0",
"comment": "someComment2"
}
],
"license3": [
{
"topic": "someText3",
"status": "1",
"action": "1",
"comment": "someComment3"
}
]
}
},
"release2": {
"attachment2": {
"license2": [
{
"topic": "someText2", // ^^ This object is missing in current output.
"status": "0",
"action": "0",
"comment": "someComment2"
},
{
"topic": "someText3",
"status": "1",
"action": "1",
"comment": "someComment3"
}
]
}
}
}

^^ 我想在 jsonObject 中包含对象数组.

有人可以帮助我调整需要在 setPath 中完成吗?在第 3 点中发挥作用以获得预期结果?

PS:我知道我问过类似的问题here .

最佳答案

您正在覆盖许可证数组,而不是仅仅附加新项目。

改变这一行

keys.reduce((r, a) => r[a] = r[a] || {}, obj)[last] =  [item];

对此

const target = keys.reduce((r, a) => r[a] = r[a] || {}, obj);
(target[last] = target[last] || []).push(item);

关于javascript - 在 javascript 中将 Map<String, Object> 转换为 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57252151/

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