JSFiddle - React, Tailwind, and code Playground

by fangunxs

JavaScript

var navigatorMenu = {
    "title": "Phases",
    "children": [
        {
            "title": "Treatment",
            "children": [
                {
                    "title": "Chemotherapy",
                    "children": [
                        {
                            "title": "Is it for Me?",
                            "url": null,
                            "currentPage": false
                        },
                        {
                            "title": "Decision#2",
                            "url": null,
                            "currentPage": false
                        },
                        {
                            "title": "Decision#3",
                            "url": null,
                            "currentPage": true
                        }
                    ],
                    "url": null,
                    "currentPage": false
                },
                {
                    "title": "Surgery",
                    "children": [
                        {
                            "title": "Is it for Me2?",
                            "url": null,
                            "currentPage": false
                        }
                    ],
                    "url": null,
                    "currentPage": false
                }
            ],
            "url": null,
            "currentPage": false
        }
    ],
    "url": null,
    "currentPage": false
};


var ancestor = [];
console.log(findLevel(navigatorMenu));
console.log("ancestor", ancestor);

function findLevel (obj) {
    if(obj["currentPage"]){
        return obj;    
    } else {
        for (var i in obj["children"]) {
            var result = findLevel(obj["children"][i]);
            if (result) {
                ancestor.push(obj);
                return result;
            }
        }
    }
}