1

I'm trying to iterate thru an object but without knowing its depth. My task is to go thru all objects and get the 'content'. if 'has_content' is false, then content does not have anything but its child/children might have content. I'm supposed to go to the last child. I am thinking of solving it by recursion. I'm pretty new to js. help is appreciated.

the result of the object below should be ["hello", "world", "last one", "level 1"].

'use strict'
let obj1 = {
    "has_content": false,
    "content": null,
    "nodes": [
        {
            "has_content": false,
            "content": null,
            "nodes": [
                {
                    "has_content": true,
                    "content": "Hello",
                    "nodes": [
                        {
                            "has_content": true,
                            "content": "world",
                            "nodes": [
                                {
                                    "has_content": true,
                                    "content": "last one"
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ], 
    "has_content": false,
    "content": null,
    "nodes": [
        {
            "has_content": true,
            "content": "level 1"
        }
    ]
}
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
mateo
  • 103
  • 1
  • 8

0 Answers0