How can I iterate through an array of objects a certain number of times to access all the child arrays within each object and retrieve their IDs in order?
let data = [{
"id": "1",
"child": [
{
"id": "12",
"child": [
{
"id": "123",
"child": [
{
"id": "1234"
}
}
]
},
{
"id": "2",
"child": [
{
"id": "22"
}
]
},
{
"id": "3"
},
{
"id": "4",
"child": [
{
"id": "42",
"child": [
{
"id": "43"
}
}
]
}
]
}]
Expected Output
[1,12,123,1234,2,22,3,4,42,43]
This is my attempt at solving it, but I am unable to find a working logic:
result.reduce((pv, cv) => {
console.log(cv)
let temp = cv
let arr = []
if(temp.hasOwnProperty("split")){
arr = temp.split
pv.push(temp.id)
// I need help with iterating through 'arr' here!
}
return pv
}, [])
If anyone has any logical steps or ideas to share, I would greatly appreciate it!