I have an array that looks like this:
files = [ 'Dashboard/Logs/Errors', 'Dashboard/Logs/Other', 'Accounts/Main', ]
I want to do this:
navigation = [ { "title": "Dashboard", "dropdown": [ { "title": "Logs", "dropdown": [ { "title": "Errors", }, { "title": "Other", } ] } ] }, { "title": "Accounts", "dropdown": [ { "title": "Main", } ] } ]
I have the following:
var navigation = []; for (var i = 0; i < files.length; i++) { var parts = files[i].split('/'); navigation.push({title: parts[0]}); for (var j = 1; j < parts.length; j++) { } }
I am having difficulty figuring out a decent way to do this. That I still donβt work, because it creates two objects under the navigation, each with title: "Dashboard" . Any ideas for a smart approach? Thanks:)
javascript
Alec fenichel
source share