I have a Tree array of objects and I want to convert it to use it into my primeReact component
This is my input data :
[
{
name: "My name",
Myid: "1",
children: [],
type: "type1",
label: "Mylabel",
function: "myfunction",
},
{
name: "Name2",
Myid: "2",
children: [],
type: "Type2",
label: "Mylabel",
function: "myfunction",
},
{
name: " Another Name",
Myid: "3",
children: ["1", "2"],
type: "this is my type",
label: "My label 4",
function: "Functionnal Component",
},
{
name: "Name3",
Myid: "5",
children: [],
type: "Type3",
label: "Mylabel3",
function: "myfunction3",
},
{
name: "Name6",
Myid: "6",
children: ["3"],
type: "Type6",
label: "Mylabel6",
function: "myfunction6",
},
];
I want to get a Tree array like this, instead of having only id's array in children field I want to have the whole object of the children, for the above axample, each field of the array should have only 3 fields : (id,data, children) and data will have name,type,label,function.
the output should look like :
const output = [
{
key: "5",
data: {
name: "Name3",
type: "Type3",
label: "Mylabel3",
function: "myfunction3",
},
children: [],
},
{
key: "6",
data: {
name: "Name6",
type: "Type6",
label: "Mylabel6",
function: "myfunction6",
},
children: [
{
key: "3",
data: {
name: " Another Name",
type: "this is my type",
label: "My label 4",
function: "Functionnal Component",
},
children: [
{
key: "1",
data: {
name: "My name",
type: "type1",
label: "Mylabel",
function: "myfunction",
},
children: [],
},
{
key: "2",
data: {
name: "Name2",
type: "Type2",
label: "Mylabel",
function: "myfunction",
},
children: [],
},
],
},
],
},
];
I've tried to convert my array but I don't have any idea how to convert it into a tree array
const manip = data?.map((el) => {
return {
key: el.Myid,
data: {
layer: el.layer,
label: el.label,
name: el.name,
type: el.type,
},
children: el.children,
};
});
console.log(manip)