Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Convert flat array objects to tree-like data #399

Open
narukeu opened this issue Apr 27, 2024 · 0 comments
Open

Feature Request: Convert flat array objects to tree-like data #399

narukeu opened this issue Apr 27, 2024 · 0 comments

Comments

@narukeu
Copy link

narukeu commented Apr 27, 2024

I would like to request a new feature that can convert flat array objects into tree-like data. I believe this is a common and interesting feature.
I believe Radash can provide a better function for constructing tree-like data (compared to writing it ourselves), and it would be beneficial for other JavaScript developers to learn.
Thank you.

For example, given the following original data,

const origin = [
  {
    id: "8ae93d4b-1e19-4638-b970-50f2a0bc9ae8",
    name: "a",
    parentId: "",
  },
  {
    id: "cbef454d-c92b-4119-82b6-1867c430b891",
    name: "aa",
    parentId: "8ae93d4b-1e19-4638-b970-50f2a0bc9ae8",
  },
  {
    id: "ae35ddc8-8606-483b-bf48-0eb5f54da3c7",
    name: "aaa",
    parentId: "cbef454d-c92b-4119-82b6-1867c430b891",
  },
  {
    id: "7f8dabc0-665f-42d3-9862-1938f12227ab",
    name: "b",
    parentId: "",
  },
  {
    id: "62bf89ae-6307-431f-b0de-e1242ba105d1",
    name: "ba",
    parentId: "7f8dabc0-665f-42d3-9862-1938f12227ab",
  },
];

I hope to get the following result:

const result = [
  {
    id: "8ae93d4b-1e19-4638-b970-50f2a0bc9ae8",
    name: "a",
    parentId: "",
    children: [
      {
        id: "cbef454d-c92b-4119-82b6-1867c430b891",
        name: "aa",
        parentId: "8ae93d4b-1e19-4638-b970-50f2a0bc9ae8",
        children: [
          {
            id: "ae35ddc8-8606-483b-bf48-0eb5f54da3c7",
            name: "aaa",
            parentId: "cbef454d-c92b-4119-82b6-1867c430b891",
          },
        ],
      },
    ],
  },
  {
    id: "7f8dabc0-665f-42d3-9862-1938f12227ab",
    name: "b",
    parentId: "",
    children: [
      {
        id: "62bf89ae-6307-431f-b0de-e1242ba105d1",
        name: "ba",
        parentId: "7f8dabc0-665f-42d3-9862-1938f12227ab",
      },
    ],
  },
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant