I have a multidimentionnal array and i want to rename property recursevly, i've tried this function
const newMenu =
Array.isArray(copyMenu) &&
copyMenu.length > 0 &&
copyMenu.map(({ families: children, id: key, label: title, ...rest }) => ({
key,
title,
...rest,
children,
}));
But it rename just the big array, not the properties inside table property This is my input array :
const input = [
{
id: 9040,
label: "33",
tables: [
{
id: 8957,
label: "THB_Adresse_Famille",
idFather: 8957,
tableIsFamily: false,
tables: []
},
{
id: 8941,
label: "THB_Contact_Famille",
idFather: 8941,
tableIsFamily: false,
tables: [
{ id: 8947, label: "THB_Contact_Table", idFather: 8941 },
{ id: 9855, label: "THB_Contact_Table_BIS", idFather: 8941 }
]
},
{
id: 8949,
label: "THB_Societe_Famille",
idFather: 8949,
tableIsFamily: false,
tables: []
},
{
id: 8983,
label: "THB_TELEPHONE",
idFather: 8983,
tableIsFamily: false,
tables: []
},
{
id: 10070,
label: "THB_TEST_5708",
idFather: 10070,
tableIsFamily: false,
tables: []
}
]
},
{
id: 9761,
label: "1111111",
tables: [
{
id: 9742,
label: "RRA PROTOCOLE",
idFather: 9742,
tableIsFamily: false,
tables: []
},
{
id: 9753,
label: "RRA TEST CASE LINE",
idFather: 9753,
tableIsFamily: false,
tables: []
}
]
},
]
The output should be ike this :
const output = [
{
key: 9040,
title: "33",
children: [
{
key: 8957,
title: "THB_Adresse_Famille",
idFather: 8957,
tableIsFamily: false,
children: []
},
{
key: 8941,
title: "THB_Contact_Famille",
idFather: 8941,
tableIsFamily: false,
children: [
{ key: 8947, title: "THB_Contact_Table", idFather: 8941 },
{ key: 9855, title: "THB_Contact_Table_BIS", idFather: 8941 }
]
},
{
key: 8949,
title: "THB_Societe_Famille",
idFather: 8949,
tableIsFamily: false,
children: []
},
{
key: 8983,
title: "THB_TELEPHONE",
idFather: 8983,
tableIsFamily: false,
children: []
},
{
key: 10070,
title: "THB_TEST_5708",
idFather: 10070,
tableIsFamily: false,
children: []
}
]
},
{
key: 9761,
title: "1111111",
children: [
{
key: 9742,
title: "RRA PROTOCOLE",
idFather: 9742,
tableIsFamily: false,
children: []
},
{
key: 9753,
title: "RRA TEST CASE LINE",
idFather: 9753,
tableIsFamily: false,
children: []
}
]
},
]
I'v searched on stackoverflow but i don't found any solutions to this case, any help will be appreciated. Thank you
idbecomeskey,labelbecomestitleandtablesbecomeschildren, correct? This may be a proper use-case for recursion. Will work on an answer.