JSBin: https://jsbin.com/kuwivohixu/edit?html,js,output
var v = {
checkit: {
'company_dummy': {
count: 0
},
'company_2': {
count: 1
},
'company_3': {
count: 1
},
'company_4': {
count: 6
},
'company_5': {
count: 6
}
}
}
var newest_company = "company_5";
var company_sorted = [];
for (var each_company in v['checkit']) { //for each company in 'v'
company_sorted.push([each_company, v['checkit'][each_company]]); //save the company and the company object in affinity
}
company_sorted.sort(function(a, b) {
return b[1]['count'] - a[1]['count'];
});
/*
company_sorted.sort(function(a,b) {
//How can I update my object that if the `newest_company` has a count
that is tied with another company as the higest `count` then move
that to be the first in the group after being sorted from high to low
})
*/
console.log('company_dummy' in v['checkit']);
console.log(company_sorted);
Как я могу обновить свой объект, чтобы, если у newest_company
был счет, связанный с другой компанией как самый высокий count
, то переместите его напервый в группе после сортировки от высокой к низкой