I am struggling with a problem. I want to Spilt javascript array to another javascript arrays using javascript. The array should be split the last element of the array. it means ['/Local1', 'Local1', 'Local'] and ['/Local2', 'Local2','Local'] should contain in Local array. ['/Forign1', 'Forign1', 'Forign'] and ['/Forign1', 'Forign1', 'Forign'] should be in Forign array.
I have mentioned the tried code below. But my code doesn't give me the expected output. What should I do to get the expected output?
exports.getMenu = function (selected, username) {
const menu = [
[
['/Local1', 'Local1', 'Local'],
['/Local2', 'Local2','Local'],
['/Forign1', 'Forign1', 'Forign'],
['/Forign2', 'Forign2','Forign'],
]
]
const xxx = getMenuGroup(selected, menu);
}
function getMenuGroup(selected, input) {
var result = input.reduce( (acc,[href,label,group]) => {
if(!acc.hasOwnProperty(group))
acc[group] = [];
acc[group].push({
href,
label,
active:(selected == href) ? ' active' : ''
});
return acc;
},{});
console.log(result);
//return result;
}
Current Output:
[
[
{ href: '/Local1', label: 'Local1', active: ' active' },
{ href: '/Local2', label: 'Local2', active: '' },
{ href: '/Forign1', label: 'Forign1', active: '' },
{ href: '/Forign2', label: 'Forign2', active: '' }
]
]
Expected output:
{
Local: [
{ href: '/Local1', label: 'Local1', active: ' active' },
{ href: '/Local2', label: 'Local2', active: '' }
],
Forign: [
{ href: '/Forign1', label: 'Forign1', active: '' },
{ href: '/Forign2', label: 'Forign2', active: '' }
],
}