我想按日期保留最高分列表:即姓名:玩家 A |成绩: 2000 |日期: 2020-04-15姓名:球员B|成绩: 2001 |日期: 2020-04-16但是,我有时会得到更高的分数,但日期更低:姓名:球员A|成绩: 2000 |日期: 2020-01-12姓名:球员B|成绩: 2001 |日期: 2019-12-20// should only return: a and c in order.let arr = [{name: c, score: 6, date: 5-20-2021},{name: b, score: 2, date: 5-20-2020},{name: a, score: 3, date: 5-20-2019}]arr.sort((a, b) => (a.score > b.score) ? 1 : -1)arr.filter((a, b) => (a.submitted > b.submitted) ? 1 : -1)
2 回答
手掌心
TA贡献1942条经验 获得超3个赞
只需在排序时输入这两个条件:
<script>
let arr = [
{name: "i", score: 16, date: 2-20-2021},
{name: "h", score: 6, date: 5-25-2021},
{name: "g", score: 6, date: 5-20-2022},
{name: "f", score: 6, date: 5-10-2021},
{name: "e", score: 16, date: 5-20-1021},
{name: "d", score: 6, date: 5-20-2001},
{name: "c", score: 6, date: 5-20-2021},
{name: "b", score: 2, date: 5-20-2020},
{name: "a", score: 3, date: 5-20-2019}
]
arr.sort((a, b) => (a.score < b.score) && (a.date < b.date) ? 1 : -1)
console.log(arr)
</script>
慕斯王
TA贡献1864条经验 获得超2个赞
filter 方法提供三个参数:
当前元素
当前元素的索引
数组
因此,您尝试将提交的与未定义进行比较。如果需要从排序数组中获取两个元素,则可以使用拼接方法
arr.sort((a, b) => b.score - a.score).splice(0, 2)
添加回答
举报
0/150
提交
取消