let d = [ {path: '/posts/', title: ' Posts ', parent: true},
{path: '/posts/create', title: ' Posts Create', parent: false},
{path: '/posts/edit', title: ' Posts Edit', parent: false},
{path: '/laravel-page', title: ' Laravel Page', parent: false},
];
function array_sort(arr) {
var cat = '';
return arr.reduce((acc, cur) => {
cur.child = [];
let path = cur.path.split('/').filter(_ => _ != "");
if(path.length === 1) {
cat = path[0];
acc.push(cur);
} else {
acc[acc.length-1].child.push(cur);
}
return acc;
}, []);
}
console.log(array_sort(d));
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.