簡體   English   中英

JavaScript:將 Json 數據數組更改為新格式

[英]JavaScript: Change Json data array into new format

我正在嘗試使用需要以多種方式更改的 json 數據。

我當前的 json 數據如下:

{
  "file1": {
    "function1": {
      "calls": {
        "105:4": {
          "file": "file2",
          "function": "function5"
        },
        "106:4": {
          "file": "file2",
          "function": "function6"
        }
      },
      "lines1": {
        "123": "102:0",
        "456": "105:8"
      },
      "lines2": {
        "102:0": [
          "102:0"
        ],
        "105:4": [
          "106:4",
          "107:1"
        ],
        "106:4": [
          "107:1"
        ]
      }
    }
  }
}

但我希望數據如下:

{
  "name": "program",
  "children": [
    {
      "name": "file1",
      "children": [
        {
          "name": "function1",
          "calls": [
            {
              "line": 105,
              "file": "file2",
              "function": "function5"
            },
            {
              "line": 106,
              "file": "file2",
              "function": "function6"
            }
          ],
          "lines1": [
            102,
            105
          ],
          "lines2": [
            [
              102,
              102
            ],
            [
              105,
              106,
              107
            ],
            [
              106,
              107
            ]
          ],
          "group": 1
        }
      ],
      "group": 1
    }
  ],
  "group": 0
}

在這里,文件和函數的數量更多。 名字的值是用戶定義的。 組信息取決於父子。 每個文件都有一個組升序組號,文件內的所有功能也將具有相同的組號。 對於 : 之前的第一部分行的值(104:4 變為 104)。

到目前為止,我已嘗試使用以下代碼,該代碼不完整且未正確處理組信息。

function build(data) {
    return Object.entries(data).reduce((r, [key, value], idx) => {
      const obj = {
        name: 'program',
        children: [],
        group: 0,
        lines: []
      }

      if (key !== 'lines2) {
        obj.name = key;
        obj.children = build(value)
          if(!(key.includes(":")))
          obj.group = idx + 1;
      } else {
        if (!obj.lines) obj.lines = [];
        Object.entries(value).forEach(([k, v]) => {
          obj.lines.push([k, ...v].map(e => e.split(':').shift()))
        })
      }

      r.push(obj)
      return r;
    }, [])
  }

  const result = build(data);
  console.log(result);

如果您能幫助我,我將不勝感激。 在此先感謝您的時間。

假設您的輸入結構如您的問題所示一致定義(即不需要“安全檢查”等),那么您可以使用Object.entries()Array.map()傳播語法如下所示。

有關如何實現該目標的詳細信息,請參閱此代碼片段中的內聯文檔:

 function transformData(data, programName) { /* Define local parse helper to extract number from NUMBER:STRING format */ const parseHelper = (str) => Number.parseInt(str.split(':')[0]); /* Define local parse helper to extract group number from STRINGNUMBER format */ const parseGroup = (str) => Number.parseInt(str.replace(/^[az]+/,"")) /* Create a root object with specified program name */ return { name : programName, /* Iterate each file name and object entry of input */ children : Object.entries(input).map(([fileName, fileObject]) => { /* Iterate function name and object of current file object */ const fileChildren = Object.entries(fileObject) .map(([functionName, functionObject]) => { /* Iterate function name and object of current file object */ const lines = Object.entries(functionObject) .reduce((target, [functionKey, functionValue]) => { if(functionKey === "calls") { /* If function key is calls, interpret this value as data to be transformed to desired calls object shape */ const calls = Object.entries(functionValue) .map(([callKey, callObject]) => { return { line : parseHelper(callKey), file : callObject['file'], function : callObject['function'] } }); /* Inject calls object into lines result */ return { ...target, calls }; } else { /* Otherwise, interpret this value as data to be transformed to desired lines object shape */ const lineValues = Object.entries(functionValue) .map(([key, value]) => { /* If value is an array, map key/value pair to a nested array in resulting linesValues array */ return Array.isArray(value) ? [key, ...value] .map(parseHelper) : parseHelper(value) }) /* Inject line values into function key of result */ return { ...target, [functionKey] : lineValues } } }, {}); /* Inject lines into function result */ return { name : functionName, ...lines, group : parseGroup(functionName) } }); /* Map file object to name/children pairing */ return { name : fileName, children : fileChildren, group : parseGroup(fileName) } }), group : 0 } } const input = { "file1": { "function1": { "calls": { "105:4": { "file": "file2", "function": "function5" }, "106:4": { "file": "file2", "function": "function6" } }, "lines1": { "123": "102:0", "456": "105:8" }, "lines2": { "102:0": [ "102:0" ], "105:4": [ "106:4", "107:1" ], "106:4": [ "107:1" ] } } } }; console.log(transformData(input, "program"))

希望有幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM