performant-array-to-tree
performant-array-to-tree copied to clipboard
Parent ID Issue
When there are multiple parents (at the same highest level) with multiple children, the result is [] empty.
const data = [
{
"Id": "51eba4ed-d724-48f6-9627-16704ef56b7a",
"LastModified": "2022-04-20T14:46:58Z",
"Title": "Navigation",
"Description": "",
"UrlName": "navigation",
"Name": "navigation",
"Synonyms": "",
"TaxonomyId": "e5cd6d69-1543-427b-ad62-688a99f5e7d4",
"Ordinal": -11,
"FullUrl": "/navigation",
"ParentId": "00000000-0000-0000-0000-000000000000",
"Provider": "OpenAccessDataProvider"
},
{
"Id": "718c5b9b-b0fd-4c84-96da-36566104efa8",
"LastModified": "2022-04-20T13:23:49Z",
"Title": "Video Type",
"Description": "",
"UrlName": "video-type",
"Name": "video-type",
"Synonyms": "",
"TaxonomyId": "e5cd6d69-1543-427b-ad62-688a99f5e7d4",
"Ordinal": -7,
"FullUrl": "/video-type",
"ParentId": "00000000-0000-0000-0000-000000000000",
"Provider": "OpenAccessDataProvider"
},
{
"Id": "3c59b053-38bd-46e3-8927-42d6320142a8",
"LastModified": "2022-04-20T14:45:59Z",
"Title": "Vimeo",
"Description": "",
"UrlName": "vimeo",
"Name": "vimeo",
"Synonyms": "",
"TaxonomyId": "e5cd6d69-1543-427b-ad62-688a99f5e7d4",
"Ordinal": -9,
"FullUrl": "/video-type/vimeo",
"ParentId": "718c5b9b-b0fd-4c84-96da-36566104efa8",
"Provider": "OpenAccessDataProvider"
},
{
"Id": "4f2a80e5-6712-4c92-a174-66b6f8786752",
"LastModified": "2022-04-05T16:13:54Z",
"Title": "In-vehicle Applications",
"Description": "",
"UrlName": "in-vehicle-applications",
"Name": "in-vehicle-applicationseaa47100-39da-44f5-9e49-2820a25fd977",
"Synonyms": "",
"TaxonomyId": "e5cd6d69-1543-427b-ad62-688a99f5e7d4",
"Ordinal": -5,
"FullUrl": "/navigation/apps/in-vehicle-applications",
"ParentId": "3b2d0dff-808a-4c38-8a49-f11fd032fc5e",
"Provider": "OpenAccessDataProvider"
},
{
"Id": "3610dbd5-4be8-415c-9d6b-94ac87f06976",
"LastModified": "2022-04-05T16:14:21Z",
"Title": "Mobile Applications",
"Description": "",
"UrlName": "mobile-applications",
"Name": "mobile-applications100b5984-8b91-4a53-b603-82bbd27ae62e",
"Synonyms": "",
"TaxonomyId": "e5cd6d69-1543-427b-ad62-688a99f5e7d4",
"Ordinal": -6,
"FullUrl": "/navigation/apps/mobile-applications",
"ParentId": "3b2d0dff-808a-4c38-8a49-f11fd032fc5e",
"Provider": "OpenAccessDataProvider"
},
{
"Id": "7d1a5c07-1b9d-421f-9902-a741c97df2fe",
"LastModified": "2022-04-20T14:45:32Z",
"Title": "YouTube",
"Description": "",
"UrlName": "youtube",
"Name": "youtube",
"Synonyms": "",
"TaxonomyId": "e5cd6d69-1543-427b-ad62-688a99f5e7d4",
"Ordinal": -8,
"FullUrl": "/video-type/youtube",
"ParentId": "718c5b9b-b0fd-4c84-96da-36566104efa8",
"Provider": "OpenAccessDataProvider"
},
{
"Id": "6af37344-49df-4af3-99d3-ad90356f5264",
"LastModified": "2022-04-20T14:46:33Z",
"Title": "Brightcove",
"Description": "",
"UrlName": "brightcove",
"Name": "brightcove",
"Synonyms": "",
"TaxonomyId": "e5cd6d69-1543-427b-ad62-688a99f5e7d4",
"Ordinal": -10,
"FullUrl": "/video-type/brightcove",
"ParentId": "718c5b9b-b0fd-4c84-96da-36566104efa8",
"Provider": "OpenAccessDataProvider"
},
{
"Id": "3b2d0dff-808a-4c38-8a49-f11fd032fc5e",
"LastModified": "2022-04-20T14:47:16Z",
"Title": "Apps",
"Description": "",
"UrlName": "apps",
"Name": "appsbeb61f16-48a0-4368-acc6-b34aa7c1804e",
"Synonyms": "",
"TaxonomyId": "e5cd6d69-1543-427b-ad62-688a99f5e7d4",
"Ordinal": -4,
"FullUrl": "/navigation/apps",
"ParentId": "51eba4ed-d724-48f6-9627-16704ef56b7a",
"Provider": "OpenAccessDataProvider"
}
];
const result = arrayToTree(data, {
id: "Id",
parentId: "ParentId",
rootParentIds: {
"00000000-0000-0000-0000-000000000000": true,
},
}
console.log(result);
// output
// [] length 0
Interesting – this seems to be due to the fact that the rootParentIds
are themselves children.
Would you be able to create a PR that fixes this issue?