1

I have a JS object. I'm trying to only the top 5 values only

console.log(Object.keys(uniqVisitorDeviceType),Object.values(uniqVisitorDeviceType));

I got

(27) ["iPhone", "Windows NT 6.1", "Windows NT 10.0", "Macintosh", "iPad", "Windows NT 6.2", "Windows NT 6.3", "X11", "compatible", "Windows NT 5.1", "Linux", "Windows", "TweetmemeBot/4.0", ") { :", "Windows NT 6.0", "User-Agent,Mozilla/5.0 ", "KHTML, like Gecko", "Unknown", "Android", "Android 7.1.1", "Android 7.1.2", "Windows NT x.y", "Windows NT 6.1) AppleWebKit/537.36 ", "Windows NT 5.0", "Windows NT 8.0", "web crawler :: robots.txt exclude elefent", "Windows NT"] 

(27) [198, 2197, 2381, 1271, 11, 46, 81, 417, 1752, 87, 225, 70, 8, 14, 6, 1, 6, 9, 1, 1, 2, 2, 7, 1, 1, 1, 1]

How do I grab the 5 values order? order ?

-2381
-2197
-1752
-1271
-417
-225
-198
.... 

console.log(JSON.stringify(uniqVisitorDeviceType));

yield this

{"iPhone":198,"Windows NT 6.1":2198,"Windows NT 10.0":2381,"Macintosh":1271,"iPad":11,"Windows NT 6.2":46,"Windows NT 6.3":81,"X11":417,"compatible":1752,"Windows NT 5.1":87,"Linux":225,"Windows":70,"TweetmemeBot/4.0":8,") { :":14,"Windows NT 6.0":6,"User-Agent,Mozilla/5.0 ":1,"KHTML, like Gecko":6,"Unknown":9,"Android":1,"Android 7.1.1":1,"Android 7.1.2":2,"Windows NT x.y":2,"Windows NT 6.1) AppleWebKit/537.36 ":7,"Windows NT 5.0":1,"Windows NT 8.0":1,"web crawler :: robots.txt exclude elefent":1,"Windows NT":1}
code-8
  • 54,650
  • 106
  • 352
  • 604
  • 2
    Can you add the original object to your question too (e.g. `console.log(JSON.stringify(uniqVisitorDeviceType))`) – AKX Apr 16 '19 at 14:54
  • copy the numeric list and sort the copy, grab the top 5 values, find the index of the top 5 values in original list, use that index to grab the corresponding string from the other list? – M Y Apr 16 '19 at 14:55
  • @AKX I added the result of `console.log(JSON.stringify(uniqVisitorDeviceType));` – code-8 Apr 16 '19 at 15:19
  • This answer can help https://stackoverflow.com/a/1069840/6048928 – RaR Apr 16 '19 at 15:24

3 Answers3

2

You can use sort() to make ordered list of values of Object. And then use slice(0,n) get top n elements.

let obj = {"iPhone":198,"Windows NT 6.1":2198,"Windows NT 10.0":2381,"Macintosh":1271,"iPad":11,"Windows NT 6.2":46,"Windows NT 6.3":81,"X11":417,"compatible":1752,"Windows NT 5.1":87,"Linux":225,"Windows":70,"TweetmemeBot/4.0":8,") { :":14,"Windows NT 6.0":6,"User-Agent,Mozilla/5.0 ":1,"KHTML, like Gecko":6,"Unknown":9,"Android":1,"Android 7.1.1":1,"Android 7.1.2":2,"Windows NT x.y":2,"Windows NT 6.1) AppleWebKit/537.36 ":7,"Windows NT 5.0":1,"Windows NT 8.0":1,"web crawler :: robots.txt exclude elefent":1,"Windows NT":1}


let res = Object.values(obj).sort((a,b) => b-a).slice(0,5);

console.log(res)
Maheer Ali
  • 35,834
  • 5
  • 42
  • 73
2

This will help:

const obj = {"iPhone":198,"Windows NT 6.1":2198,"Windows NT 10.0":2381,"Macintosh":1271,"iPad":11,"Windows NT 6.2":46,"Windows NT 6.3":81,"X11":417,"compatible":1752,"Windows NT 5.1":87,"Linux":225,"Windows":70,"TweetmemeBot/4.0":8,") { :":14,"Windows NT 6.0":6,"User-Agent,Mozilla/5.0 ":1,"KHTML, like Gecko":6,"Unknown":9,"Android":1,"Android 7.1.1":1,"Android 7.1.2":2,"Windows NT x.y":2,"Windows NT 6.1) AppleWebKit/537.36 ":7,"Windows NT 5.0":1,"Windows NT 8.0":1,"web crawler :: robots.txt exclude elefent":1,"Windows NT":1};

const top5Values = Object.values(obj).sort((a,b) =>b-a).slice(0,5);

const top5Keys = Object.keys(obj).sort((a,b) => obj[b]- obj[a]).slice(0,5);

console.log(top5Keys, top5Values)
Zeyad Etman
  • 2,250
  • 5
  • 25
  • 42
1

You can use

  • Object.entries() to get string/number pairs from your data,
  • sort them with a custom comparison function that looks at the numbers within the pairs
  • use .slice(0, 5) to limit things to the top 5

The return value is an array of 2-arrays.

const data = {
    "iPhone": 198,
    "Windows NT 6.1": 2198,
    "Windows NT 10.0": 2381,
    "Macintosh": 1271,
    "iPad": 11,
    "X11": 417,
    "compatible": 1752,
    // ... snip ...
};

const top5 = Object.entries(data).sort((a, b) => b[1] - a[1]).slice(0, 5);

console.log(top5);

outputs

[ [ 'Windows NT 10.0', 2381 ],
  [ 'Windows NT 6.1', 2198 ],
  [ 'compatible', 1752 ],
  [ 'Macintosh', 1271 ],
  [ 'X11', 417 ] ]
AKX
  • 152,115
  • 15
  • 115
  • 172