如何根据组和升序重新排列多个级别的对象?

问题描述 投票:0回答:1

我需要将链式对象重新排列为非链式对象。因此,我需要创建更多对象来表示这一点。

基本上我有这个:

1. Person (code:1) (level 1)
 1.2 Person (code:2) (level 2)
  1.2.1 Person (code:3) (level 3)
 1.3 Person (code:4) (level 2)
  1.3.1 Person (code:5) (level 3)
  1.3.2 Person (code:6) (level 3)

并且,期待这个:

  1. 人(代码:1)(1级)
  2. 人(代码:2)(2级)
  3. 人(代码:3)(3级)
  4. 人(代码:4)(2级)
  5. 人(代码:5)(3级)
  6. 人(代码:6)(3级)

现在,在现实世界中。我的目标:

[
    {
        "id": 5,
        "name": "Jhon",
        "code": "2",
        "employee_lvl2": [
            {
                "id": 23,
                "name": "Rodolf (John's employee 1)",
                "code": "9",
                "employee_lvl3": [
                    {
                        "id": 5,
                        "name": "Marcus (Rodolf's employee 1)",
                        "code": "18"
                    }
                ]
            },
            {
                "id": 17,
                "name": "Samuel (John's employee 2)",
                "code": "16",
                "employee_lvl3": [
                    {
                        "id": 11,
                        "name": "Jacob (Samuel's employee 1)",
                        "code": "21"
                    },
                    {
                        "id": 18,
                        "name": "Agnes (Samuel's employee 2)",
                        "code": "31"
                    }
                ]
            }
        ]
    },
    {
        "id": 4,
        "name": "Jennifer",
        "code": "1",
        "employee_lvl2": [
            {
                "id": 8,
                "name": "James (Jennifer's employee 1)",
                "code": "3",
                "employee_lvl3": [
                    {
                        "id": 12,
                        "name": "Jonathan (James's employee 1)",
                        "code": "8"
                    },
                    {
                        "id": 8,
                        "name": "Agnes (James's employee 2)",
                        "code": "3"
                    }
                ]
            },
            {
                "id": 7,
                "name": "Julie (Jennifer's employee 2)",
                "code": "6",
                "employee_lvl3": [
                    {
                        "id": 1,
                        "name": "Jacob (Julie's employee 1)",
                        "code": "1"
                    }
                ]
            }
        ]
    }
]

我期望类似:(按所有级别中的代码排序并在属性“stacked_code”中堆叠代码)

[
    {
        "id": 4,
        "name": "Jennifer",
        "code": "1"
    },
    {
        "id": 8,
        "name": "James (Jennifer's employee 1)",
        "code": "3",
        "stacked_code": "1.3"
    },
    {
        "id": 8,
        "name": "Agnes (James's employee 2)",
        "code": "3",
        "stacked_code": "1.3.3"
    },
    {
        "id": 12,
        "name": "Jonathan (James's employee 1)",
        "code": "8",
        "stacked_code": "1.3.8"
    },
    {
        "id": 7,
        "name": "Julie (Jennifer's employee 2)",
        "code": "6",
        "stacked_code": "1.6"
    },
    {
        "id": 1,
        "name": "Jacob (Julie's employee 1)",
        "code": "1",
        "stacked_code": "1.6.1"
    },
    {
        "id": 5,
        "name": "Jhon",
        "code": "2"
    },
    {
        "id": 23,
        "name": "Rodolf (John's employee 1)",
        "code": "9",
        "stacked_code": "2.9"
    },
    {
        "id": 5,
        "name": "Marcus (Rodolf's employee 1)",
        "code": "18",
        "stacked_code": "2.9.18"
    },
    {
        "id": 17,
        "name": "Samuel (John's employee 2)",
        "code": "16",
        "stacked_code": "2.16"
    },
    {
        "id": 11,
        "name": "Jacob (Samuel's employee 1)",
        "code": "21",
        "stacked_code": "2.16.21"
    },
    {
        "id": 18,
        "name": "Agnes (Samuel's employee 2)",
        "code": "31",
        "stacked_code": "2.16.31"
    }
]

我尝试使用一些map()和push数组,但没有什么意义。 我知道这在某种程度上可以与reduce配合使用,但我现在无法控制自己。

javascript reactjs arrays object reduce
1个回答
0
投票

可以使用递归函数来解决这个问题。

const outputArr = [];

/**
 * Convert a multilevel person object into one level
 * @param {Object} person The person object
 */
const convertToOneLevel = (person, parentCode = null) => {
  const newPerson = {
    id: person.id,
    name: person.name,
    code: person.code,
    stacked_code: parentCode ? `${parentCode}.${person.code}` : undefined,
  };
  outputArr.push(newPerson);
  const idx = Object.keys(person).findIndex((key) =>
    key.startsWith('employee_lvl')
  );
  if (idx !== -1) {
    person[Object.keys(person)[idx]].map((p) =>
      convertToOneLevel(
        p,
        newPerson.stacked_code ? newPerson.stacked_code : newPerson.code
      )
    );
  }
};

obj.forEach((p) => convertToOneLevel(p));

那里

obj
是您的输入对象。

© www.soinside.com 2019 - 2024. All rights reserved.