【问题标题】:Convert nested object into hierarchy data [closed]将嵌套对象转换为层次结构数据[关闭]
【发布时间】:2022-11-24 03:35:35
【问题描述】:

我正在尝试转换此嵌套对象结构以将其与 react ag-grid 一起使用。

[
  {
    "data": {
      "ID": "44",
      "Name": "Trillian",
      "Gender": "female",
      "Ability": "mathematician",
      "Minimal distance": "6.2000000000",
      "Weight": "49",
      "Born": "Mon Dec 14 00:00:00 CET 1994",
      "In space since": "Wed Dec 24 17:21:50 CET 2014",
      "Beer consumption (l/y)": "6704",
      "Knows the answer?": "true"
    },
    "children": {
      "has_nemesis": {
        "records": [
          {
            "data": {
              "ID": "1007",
              "Character ID": "44",
              "Is alive?": "true",
              "Years": "29"
            },
            "children": {
              "has_secrete": {
                "records": [
                  {
                    "data": {
                      "ID": "2008",
                      "Nemesis ID": "1007",
                      "Secrete Code": "1799820570"
                    },
                    "children": {}
                  }
                ]
              }
            }
          }
        ]
      }
    }
  },

进入这个子父模式,我可以使用 ag 网格来创建层次结构数据表

[
  {
    "employeeId": 101,
    "employeeName": "Erica Rogers",
    "jobTitle": "CEO",
    "employmentType": "Permanent",
    "children": [
      {
        "employeeId": 102,
        "employeeName": "Malcolm Barrett",
        "jobTitle": "Exec. Vice President",
        "employmentType": "Permanent",
        "children": [
          {
            "employeeId": 103,
            "employeeName": "Esther Baker",
            "jobTitle": "Director of Operations",
            "employmentType": "Permanent",
            "children": [
              {
                "employeeId": 104,
                "employeeName": "Brittany Hanson",
                "jobTitle": "Fleet Coordinator",
                "employmentType": "Permanent",
                "children": [
                  {
                    "employeeId": 105,
                    "employeeName": "Leah Flowers",
                    "jobTitle": "Parts Technician",
                    "employmentType": "Contract"
                  },
                  {
                    "employeeId": 106,
                    "employeeName": "Tammy Sutton",
                    "jobTitle": "Service Technician",
                    "employmentType": "Contract"
                  }
                ]
              },
              {
                "employeeId": 107,
                "employeeName": "Derek Paul",
                "jobTitle": "Inventory Control",
                "employmentType": "Permanent"
              }
            ]
          },
          {
            "employeeId": 108,
            "employeeName": "Francis Strickland",
            "jobTitle": "VP Sales",
            "employmentType": "Permanent",
            "children": [
              {
                "employeeId": 109,
                "employeeName": "Morris Hanson",
                "jobTitle": "Sales Manager",
                "employmentType": "Permanent"
              },
              {
                "employeeId": 110,
                "employeeName": "Todd Tyler",
                "jobTitle": "Sales Executive",
                "employmentType": "Contract"
              },
              {
                "employeeId": 111,
                "employeeName": "Bennie Wise",
                "jobTitle": "Sales Executive",
                "employmentType": "Contract"
              },
              {
                "employeeId": 112,
                "employeeName": "Joel Cooper",
                "jobTitle": "Sales Executive",
                "employmentType": "Permanent"
              }
            ]
          }
        ]
      },
]
}]

const iterate = (obj) => {
  Object.keys(obj).forEach((key) => {
    console.log(obj[key].records);

    // iterate(obj[key].records[0].data);
  });
};

const filterData = (length) => {
  if (length <= -1) {
    return;
  }
  let newObject = {};
  // console.log(data[length].children);
  newObject = { ...data[length].data };

  iterate(data[length].children);
  length--;
  filterData(length);
};

filterData(data.length - 1);

我正在尝试这个,但无法获得预期的结果。我想将每个数据属性转换为该数据属性中的父项和所有子项。

【问题讨论】:

  • 输入和输出似乎完全无关。 employeeIdemployeeName 来自哪里?请创建一个带有实际预期输出的minimal reproducible example

标签: javascript algorithm recursion data-structures javascript-objects


【解决方案1】:

我相信你想要这样的东西:

const convert = (input) => input .map (({data, children}) => ({
  ...data,
  children: Object .values (children) .map (({records = []}) => convert (records))
}))

const input = [{data: {ID: "44", Name: "Trillian", Gender: "female", Ability: "mathematician", "Minimal distance": "6.2000000000", Weight: "49", Born: "Mon Dec 14 00:00:00 CET 1994", "In space since": "Wed Dec 24 17: 21: 50 CET 2014", "Beer consumption (l/y)": "6704", "Knows the answer?": "true"}, children: {has_nemesis: {records: [{data: {ID: "1007", "Character ID": "44", "Is alive?": "true", Years: "29"}, children: {has_secrete: {records: [{data: {ID: "2008", "Nemesis ID": "1007", "Secrete Code": "1799820570"}, children: {}}]}}}]}}}]

console .log (convert (input))
.as-console-wrapper {max-height: 100% !important; top: 0}

在这里,我们获取输入数组中的每个对象,并从中创建一个包含整个 data 子对象的新记录,添加一个 children 节点,该节点是使用每个元素的 records 属性重复出现的结果children 子对象。


为了将来在 StackOverflow 上参考,请访问 Help Center,使用 tour 并在此处阅读 asking good questions

尤其是看看如何创建 Minimal, Complete, and Verifiable example

这个问题的大问题是你的样本输入和样本输出都是大数据转储。下次,策划他们。您只需要每个对象的几个属性来演示正在发生的事情。但更重要的是,确保您请求的输出与给定的输入实际匹配。我想我猜对了你想要什么,但我不应该猜测。匹配良好的小输入和输出消除了猜测。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-10
    • 2021-08-08
    • 2021-03-13
    • 1970-01-01
    • 1970-01-01
    • 2013-03-08
    • 1970-01-01
    相关资源
    最近更新 更多