Reputation: 81
is it posible to group object and combine all properties with Underscore.js like this:
[
{ menu: "Setting", role: "admin" },
{ menu: "Setting", role: "user" },
{ menu: "Setting", role: "developer" },
{ menu: "Application", role: "admin" },
{ menu: "Application", role: "user" },
]
into something like this:
[
{ menu: "Setting", admin: "OK", user: "OK", developer: "OK"},
{ menu: "Application", admin: "OK", user: "OK"},
]
Upvotes: 0
Views: 50
Reputation: 2856
_.map(_.groupBy(arr, 'menu'), function(roles, menu) {
var entry = {menu: menu};
_.each(roles, function(role) { entry[role.role] = "OK"; });
return entry;
})
I'm assuming the output you are after is:
[
{ "menu": "Setting", "admin": "OK", "user": "OK", "developer": "OK" },
{ "menu": "Application", "admin": "OK", "user": "OK" }
]
Upvotes: 1