Castromen,
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
}
}
},
"user 4": {
company: 'Копыта и рога',
name: 'Иван',
projects: {
projectID1: {
role_right: 1
}
}
}
};
var newData = Object.values(data).reduce(function(object, user) {
var obj = {
role_right: user.projects.projectID1.role_right,
company: user.company,
name: user.name
},
item;
if (user.company in object === false) {
item = {
title: user.company,
data: [obj]
};
object[user.company] = item.data;
object.newData.push(item)
} else {
var arr = object[user.company];
for (var i = arr.length - 1; i ; i--) {
item = arr[i];
if (item.role_right < obj.role_right) break;
}
arr.splice(++i, 0, obj)
}
return object
}, {newData: []}).newData;
console.log(newData);