(() => {
const roleRight = [
{
"Id": 1,
"RoleId": 1,
"RightId": 1
},
{
"Id": 2,
"RoleId": 1,
"RightId": 2
},
{
"Id": 3,
"RoleId": 2,
"RightId": 2
},
{
"Id": 4,
"RoleId": 3,
"RightId": 1
},
];
const roles = [
{
"Id": 1,
"Name": "Администратор",
},
{
"Id": 2,
"Name": "Модератор",
},
{
"Id": 3,
"Name": "Пользователь",
},
];
const rights = [
{
"Id": 1,
"Name": "Контакты - Просмотр",
},
{
"Id": 2,
"Name": "Контакты - Редактирование",
}
];
const usersRoles = (() => {
const dictionaries = {
roles,
rights
};
const dictionariesIndexed = Object.keys(dictionaries).reduce((res, key) => {
res[key] = {};
dictionaries[key].forEach(item => {
res[key][item.Id] = item;
});
return res;
}, {});
const result = roleRight.reduce((res, item) => {
res[item.RoleId] = {
Id: item.RoleId,
Name: dictionariesIndexed.roles[item.RoleId].Name,
Rights: []
};
return res;
}, {});
roleRight.forEach(item => {
const roleId = item.RoleId;
const rightId = item.RightId;
result[item.RoleId].Rights.push({
Id: item.RightId,
Name: dictionariesIndexed.rights[item.RightId].Name
});
});
return Object.values(result);
})();
console.log(usersRoles);
})();