I want to output only the roles that contain the same group as the user.
// User can be part of many groups
const user = {
groups: ["group2"]
};
// Roles can have many groups
// What you see here is the output or 2 different data source
// Thats why we have group duplication inside different role
const roles = [{
name: "role1",
groups: [{
id: "group1"
}]
}, {
name: "role2",
groups: [{
id: "group1"
}, {
id: "group2"
}]
}];
const result = roles.filter(role => role.groups.filter(group => user.groups.indexOf(group.id) > -1).length)
console.log(result);
Is there a better way by using reduce or something else?
roles.filter(role => role.groups.find(group => user.groups.includes(group.id)))
– Xotic750 Sep 20 '16 at 18:15const roles
and then you try to redefine it. – Xotic750 Sep 20 '16 at 18:35