JSFiddle - React, Tailwind, and code Playground

by davidcl64

JavaScript

var obj = {
    "pages": [{
        "title": "title one",
            "sub": [{
            "title": "title one one"
        }, {
            "title": "title one two"
        }]
    }, {
        "title": "title two"
    }, {
        "title": "title three",
            "sub": [{
            "title": "title three one"
        }, {
            "title": "title three two"
        }, {
            "title": "title three three"
        }]
    }]
};

function add_slug(array_of_obj)
{
    for(var pageIdx = 0; pageIdx < array_of_obj.length; ++pageIdx) {
        var curPage = array_of_obj[pageIdx];
        
        if(curPage.title) {
            curPage.slug = curPage.title.split(' ').join('-');
        }
        
        if(curPage.sub) {
            add_slug(curPage.sub);
        }
    }
}

 add_slug(obj.pages);

console.log(JSON.stringify(obj, null, 2));