2 回答

TA貢獻(xiàn)1942條經(jīng)驗(yàn) 獲得超3個(gè)贊
只需在排序時(shí)輸入這兩個(gè)條件:
<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貢獻(xiàn)1864條經(jīng)驗(yàn) 獲得超2個(gè)贊
filter 方法提供三個(gè)參數(shù):
當(dāng)前元素
當(dāng)前元素的索引
數(shù)組
因此,您嘗試將提交的與未定義進(jìn)行比較。如果需要從排序數(shù)組中獲取兩個(gè)元素,則可以使用拼接方法
arr.sort((a, b) => b.score - a.score).splice(0, 2)
添加回答
舉報(bào)