我試圖按average
屬性按降序對對象數組排序 - 所以最大的average
是第一個 - 但不能使用underscore.js。下面是我的嘗試:使用underscore.js按值排序對象數組
var jsonData = [
{
"title": "Dear Kitten",
"totalCount": 1689,
"average": 241
},
{
"title": "Weird Things All Couples Fight About",
"totalCount": 9966,
"average": 1424
},
{
"title": "If Disney Princesses Were Real",
"totalCount": 16567,
"average": 2367
},
{
"title": "Secret Tricks With Everyday Objects",
"totalCount": 24884,
"average": 3555
},
{
"title": "The Coolest Travel Hacks",
"totalCount": 41847,
"average": 8369
},
{
"title": "5 Ways You're Drinking Coffee Wrong",
"totalCount": 55673,
"average": 7953
},
{
"title": "The Perfect Way To Pour A Beer",
"totalCount": 58097,
"average": 58097
},
{
"title": "Fruit You're Eating Wrong",
"totalCount": 65570,
"average": 32785
},
{
"title": "Your Cat Is Judging You",
"totalCount": 78952,
"average": 11279
},
{
"title": "3rd Date vs 30th Date",
"totalCount": 84394,
"average": 14066
}
];
console.log(_.sortBy(jsonData, "average"));
??看起來它工作得很好。結果按「平均」屬性值的順序排序。 – Pointy
你不在你的小提琴中包括下劃線:http://jsfiddle.net/y7h9cu6p/1/ –
@RobM。 ?當我剛剛嘗試時,它就在那裏。 (這是OP的小提琴中的「外部資源」。) – Pointy