I have following data:
const section={
fileds: [
{
child: [
{
fileds: [
{
id: "kxf5",
label: null
}
]
},
{
fileds: [
{
id: "ed5t",
label: "section"
}
]
},
]
},
{
child: [
{
fileds: [
{
id: "ccfr",
label: null
}
]
},
{
fileds: [
{
id: "kdpt8",
label: "section"
}
]
},
]
},
]
}
I need to return all id in array and I should use recursion.
I have try following code.
const section={fileds: [{child: [{fileds: [{id: "kxf5",label: null}]},{fileds: [{id: "ed5t",label: "section"}]},]},{child: [{fileds: [{id: "ccfr",label: null}]},{fileds: [{id: "kdpt8",label: "section"}]},]},]}
function printId(value, child ) {
return [value, ...(child ? printList(child) : [])]
}
console.log(printId(section.fields));
But it not helped me.
Is there a way to solve this problem with recursion? Please help to fix this.
The final result should be like ["kxf5","ed5t","ccfr", "kdpt8"] this.
fileds.