Reputation: 1572
I have
const menu = ['home', 'news', 'about'];
I want to map it to this:
let menuExt =
{
home: Common.locs['home'],
news: Common.locs['news'],
about: Common.locs['about']
};
How do I do that? I tried
let menuExt = menu.map(item => {
return {
item: Common.locs[item]
}
});
but I got an array with "item" as property, but I want one object with properties home, news, about... (there are many more but I shortened it here)
Upvotes: 1
Views: 134
Reputation: 5303
The idiomatic way would be to use Array.reduce
, because you're taking an array of objects and returning a single object.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce
const menu = ['home', 'news', 'about'];
const menuExt = menu.reduce((acc, key) => {
acc[key] = Common.locs[key];
return acc;
}, {});
Upvotes: 0
Reputation: 1572
I managed this way, but I don't know if there is a cleaner and faster solution maybe:
let menuExt = {}
menu.forEach((item) => {
menuExt[item] = Common.locs[item]
});
Upvotes: 0