I have array like below , I want to sort it by key and then remove everything except last 2 items and delete remaining.
var status = new Array();
status.push({key: 'BOB', value: 10});
status.push({key: 'TOM', value: 3});
status.push({key: 'ROB', value: 22});
status.push({key: 'JON', value: 7});
If I again push below with duplicate key for example :
status.push({key: 'BOB', value: 20});
I need following output , how do i achieve this in javascript.
[
{
"key": "BOB",
"value": 20
},
{
"key": "TOM",
"value": 3
},
{
"key": "ROB",
"value": 22
},
{
"key": "JON",
"value": 7
}
]
Note : I need to sort this by key later.
Edit : If I have object like this , How do i sort by keys ? and get only last 2 items and delete remaining.
var status = new Object();
status['BOB'] = 10
status['TOM'] = 3
status['ROB'] = 22
status['JON'] = 7