将扁平的 JSON 转换为带有子项的 JSON

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

Flat JSON to JSON with children

问题

这是您的代码,我将只翻译代码部分,不包括注释:

// 创建顶层节点的根数组
const root = [];

sideNavData.forEach((node, index) => {
    const nodeLevel = node.metaData.level;

    if (root.length === 0) {
        return root.push(node)
    }

    const previousLevel = root[index - 1].metaData.level;

    if (previousLevel === nodeLevel) {
        return root.push(node)
    } else {
        return root[index - 1]['subNav'] = node
    }
});

console.log(root);

如果您需要更多帮助或改进建议,请告诉我。

英文:

I have a JSON containing the following structure:

const content = [
  { title: "Item 1", metaData: { "level": 1, "desc": "Some Desc 1", "displayOnOverview": true }},
  { title: "Item 2", metaData: { "level": 2, "desc": "Some Desc 2", "displayOnOverview": true }},
  { title: "Item 3", metaData: { "level": 2, "desc": "Some Desc 3", "displayOnOverview": false }},
  { title: "Item 4", metaData: { "level": 3, "desc": "Some Desc 4", "displayOnOverview": true }},
  { title: "Item 5", metaData: { "level": 1, "desc": "Some Desc 5", "displayOnOverview": true }}
];

and from the above, I need to build the following output:

 {
  "title": "Item 1",
  "metaData": {
    "desc": "Some Desc 1",
    "displayOnOverview": true
  },
  "subNav": [
    {
      "title": "Item 2",
      "metaData": {
        "desc": "Some Desc 2",
        "displayOnOverview": true
      }
    },
    {
      "title": "Item 3",
      "metaData": {
        "desc": "Some Desc 3",
        "displayOnOverview": false
      },
      "subNav": [
        {
          "title": "Item 4",
          "metaData": {
            "desc": "Some Desc 4",
            "displayOnOverview": true
          }
        }
      ]
    }
  ]
},
{
  "title": "Item 6",
  "metaData": {
    "desc": "Some Desc 6",
    "displayOnOverview": true
  }
}

I have written some code (shown below) and it works but just for the top level, as soon as it finds "title": "Item 3" it stops working, pretty sure I am missing something:

    // Create root for top-level node(s)
    const root = [];

    sideNavData.forEach((node, index) => {
        const nodeLevel = node.metaData.level;

        if (root.length === 0) {
            return root.push(node)
        }

        const previousLevel = root[index - 1].metaData.level;

        if (previousLevel === nodeLevel) {
            return root.push(node)
        } else {
            return root[index - 1]['subNav'] = node
        }
    });

    console.log(root);

Can I get some ideas on how to achieve this?

答案1

得分: 1

以下是您要翻译的内容:

const content = [
  { title: "Item 1", metaData: { "level": 1 }},
  { title: "Item 2", metaData: { "level": 2 }},
  { title: "Item 3", metaData: { "level": 2 }},
  { title: "Item 4", metaData: { "level": 3 }},
  { title: "Item 5", metaData: { "level": 1 }}
];

function buildNestedStructure(data) {
  const topLevelItems = [];
  const itemMap = {};

  for (const item of data) {
    const { title, metaData } = item;
    const newItem = { title, metaData };

    if (metaData.level === 1) {
      topLevelItems.push(newItem);
    } else {
      const parentLevel = metaData.level - 1;
      const parentItem = itemMap[parentLevel];

      if (!parentItem.subNav) {
        parentItem.subNav = [];
      }

      parentItem.subNav.push(newItem);
    }

    itemMap[metaData.level] = newItem;
  }

  return topLevelItems;
}

const output = buildNestedStructure(content);
console.log(JSON.stringify(output, null, 2));

希望这对您有帮助!

英文:

Here you are:

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

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

const content = [
  { title: &quot;Item 1&quot;, metaData: { &quot;level&quot;: 1 }},
  { title: &quot;Item 2&quot;, metaData: { &quot;level&quot;: 2 }},
  { title: &quot;Item 3&quot;, metaData: { &quot;level&quot;: 2 }},
  { title: &quot;Item 4&quot;, metaData: { &quot;level&quot;: 3 }},
  { title: &quot;Item 5&quot;, metaData: { &quot;level&quot;: 1 }}
];

function buildNestedStructure(data) {
  const topLevelItems = [];
  const itemMap = {};

  for (const item of data) {
    const { title, metaData } = item;
    const newItem = { title, metaData };

    if (metaData.level === 1) {
      topLevelItems.push(newItem);
    } else {
      const parentLevel = metaData.level - 1;
      const parentItem = itemMap[parentLevel];

      if (!parentItem.subNav) {
        parentItem.subNav = [];
      }

      parentItem.subNav.push(newItem);
    }

    itemMap[metaData.level] = newItem;
  }

  return topLevelItems;
}

const output = buildNestedStructure(content);
console.log(JSON.stringify(output, null, 2));

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月1日 01:25:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76375960.html
匿名

发表评论

匿名网友

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

确定