1 回答

TA貢獻1951條經驗 獲得超3個贊
// 隨機生成數(shù)據
// let rand = () => Math.floor(Math.random() * 100)
// let arr = 'ABCDEFG'.split('').map(e => {
// return {
// name: e,
// health: rand(),
// experience: rand(),
// security: rand(),
// }
// })
// console.log(arr)
// 這是隨機生成的一組數(shù)據
let arr = [ { name: 'A', health: 67, experience: 78, security: 88 },
{ name: 'B', health: 14, experience: 40, security: 32 },
{ name: 'C', health: 91, experience: 31, security: 64 },
{ name: 'D', health: 7, experience: 64, security: 26 },
{ name: 'E', health: 68, experience: 69, security: 77 },
{ name: 'F', health: 91, experience: 44, security: 43 },
{ name: 'G', health: 61, experience: 44, security: 68 } ]
// 排序
let ret = arr
.sort((a, b) => {
return b.health - a.health
})
.slice(0, 3)
.sort((a, b) => {
return b.experience - a.experience
})
.slice(0, 2)
.sort((a, b) => {
return b.security - a.security
})
.shift()
console.log(ret)
// { name: 'E', health: 68, experience: 69, security: 77 }
添加回答
舉報