Reputation: 691
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.
Upvotes: 0
Views: 77
Reputation: 2066
A better solution could be this one:
// recursive method to extract "id"s
function getId(o) {
// if current parameter "o" has "id" prop, return it
if (o.id) return o.id;
// else, iterate over "o" and recurse for each "value" of "o"
return Object.keys(o).flatMap((key) => getId(o[key]));
}
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"
}]
},
]
},
]
};
console.log(getId(section));
Upvotes: 1
Reputation: 44073
An other way, using reduce()
to recursively fill an array
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) {
return value.reduce((prev, cur) => {
if (cur.id) {
return [ ...prev, cur.id ];
} else {
let key = ('child' in cur) ? 'child' : 'fileds';
return [ ...prev, ...printId(cur[key]) ]
}
}, []);
}
console.log(printId(section.fileds));
Upvotes: 1
Reputation: 3691
Below is one possible way to achieve the target.
Code Snippet
// recursively get only "id"s
const getIds = arr => (
// implicit return of below result
// iterate using ".flatMap()" to avoid nested result
arr.flatMap(obj => {
// extract values that are arrays
const arrArrs = Object.values(obj).filter(v => Array.isArray(v));
// extract values for key "id"
const arrVals = Object.entries(obj).filter(
([k, v]) => typeof v === 'string' && k === "id"
).map(([k, v]) => v);
// return "id" values, and then recursively call getIds for each "ar" (array)
return [...arrVals, ...arrArrs.flatMap(ar => getIds(ar))]
})
);
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"
}]
},
]
},
]
};
console.log(getIds(section.fileds));
Explanation
Inline comments added in the snippet above.
Upvotes: 0