在JavaScript中将Map <String,Object>转换为json

时间:2019-07-29 10:43:09

标签: javascript json

我有一个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.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:迭代地图并填充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中拥有对象数组。

有人可以通过第3点中的setPath函数来完成调整吗?

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

1 个答案:

答案 0 :(得分:2)

您将覆盖许可证阵列,而不仅仅是追加新项。

更改此行

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);