var data = {
"user 1": {
company: 'Рога и копыта',
name: 'Александр',
projects: {
projectID1: {
role_right: 0
}
}
},
"user 2": {
company: 'Копыта и рога',
name: 'Петя',
projects: {
projectID1: {
role_right: 0
}
}
},
"user 3": {
company: 'Копыта и рога',
name: 'Василий',
projects: {
projectID1: {
role_right: 2
}
}
},
};
var temp = Object.values(data).reduce((object, user) => {
if(user.company in object === false) {
object[user.company] = {
title: user.company,
data: []
};
}
object[user.company].data.push(user);
return object;
}, {});
var newData = Object.values(temp).map(company => {
company.data.sort((a, b) => a.projects.projectID1.role_right > b.projects.projectID1.role_right ? 1 : -1);
company.data = company.data.map(user => {
user.role_right = user.projects.projectID1.role_right;
delete user.projects; // возможно не надо удалить?
return user;
});
return company;
});
console.log(newData);