I have 2 Arrays:
var links = [
{
code: 'home'
},
{
code: 'contact'
},
];
var subLinks = [
{
code: 'some subLink',
parent: {
code: 'home'
}
},
{
code: 'some subLink 2',
parent: {
code: 'home'
}
},
{
code: 'some subLink 3',
parent: {
code: 'contact'
}
}
];
I need to have Object (link as key with array of subLinks):
var menu = {
home: ["some subLink", "some subLink 2"],
contact: ["some subLink 3"]
};
At this moment I have this...
links.map(link => ({
[link.code]: subLinks.map(subLink => (subLink.parent.code === link.code && subLink.code))
}))
mapwill return an array. Consider usingreduce.