如何从JS对象获取前5个属性?

时间:2019-04-16 14:51:40

标签: javascript jquery json sorting slice

我有一个JS对象。我只尝试只使用前5个值

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

我知道了

(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]

如何获取5个值的顺序?订购?

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

  

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

产生

{"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}

3 个答案:

答案 0 :(得分:2)

您可以使用sort()列出Object的值的有序列表。然后使用slice(0,n)获取前n个元素。

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)

答案 1 :(得分:1)

这将有助于:

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)

答案 2 :(得分:1)

您可以使用

  • Object.entries()从数据中获取字符串/数字对,
  • 使用自定义比较功能对它们进行排序,该功能可以查看对中的数字
  • 使用.slice(0, 5)将事情限制在前5位

返回值是一个由2个数组组成的数组。

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);

输出

[ [ 'Windows NT 10.0', 2381 ],
  [ 'Windows NT 6.1', 2198 ],
  [ 'compatible', 1752 ],
  [ 'Macintosh', 1271 ],
  [ 'X11', 417 ] ]