Skip to content

Instantly share code, notes, and snippets.

@tedshd
Created November 15, 2020 19:06
Show Gist options
  • Save tedshd/2eaaa40cec7102b29d7db8ffba509fea to your computer and use it in GitHub Desktop.
Save tedshd/2eaaa40cec7102b29d7db8ffba509fea to your computer and use it in GitHub Desktop.
topnine
var data = {
'123': {
'count': 123,
'type': 'video',
'source': ''
},
'345': {
'count': 345,
'type': 'video',
'source': ''
},
'99': {
'count': 99,
'type': 'image',
'source': ''
},
'1': {
'count': 1,
'type': 'video',
'source': ''
},
'9786': {
'count': 9786,
'type': 'image',
'source': ''
},
'347': {
'count': 347,
'type': 'video',
'source': ''
},
};
// Object.keys(data);
function topNine (data) {
var arr = [];
tmp = Object.keys(data).reverse(),
l = (tmp.length >= 9) ? 9 : tmp.length;
for (let i = 0; i < l; i++) {
arr.push(data[tmp[i]]);
}
return arr;
}
console.log(topNine(data));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment