【问题标题】:How can I get top 5 properties from a JS object?如何从 JS 对象中获取前 5 个属性?
【发布时间】:2019-09-06 17:24:46
【问题描述】:

我有一个 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}

【问题讨论】:

  • 您能否也将原始对象添加到您的问题中(例如console.log(JSON.stringify(uniqVisitorDeviceType))
  • 复制数字列表并排序复制,抓取前5个值,找到原始列表中前5个值的索引,使用该索引从另一个列表中抓取对应的字符串?跨度>
  • @AKX 我添加了console.log(JSON.stringify(uniqVisitorDeviceType));的结果
  • 这个答案可以帮助stackoverflow.com/a/1069840/6048928

标签: javascript jquery json sorting slice


【解决方案1】:

您可以使用sort() 来制作对象值的有序列表。然后使用slice(0,n) get top 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)

【讨论】:

  • 我的不是数组而是对象。你能调整你的答案来适应吗?
  • 我已将其添加到我的帖子中。
  • @kyo 您能否添加前五名的预期输出。你想要数字还是键?
【解决方案2】:

这会有所帮助:

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)

【讨论】:

  • 有趣的答案。谢谢??
【解决方案3】:

你可以使用

  • Object.entries() 从您的数据中获取字符串/数字对,
  • 使用自定义比较函数对它们进行排序,该函数会查看配对中的数字
  • 使用.slice(0, 5) 将内容限制在前 5 位

返回值是一个 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);

输出

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-19
    • 1970-01-01
    • 1970-01-01
    • 2021-07-16
    • 2019-12-04
    • 1970-01-01
    • 2016-09-27
    相关资源
    最近更新 更多