第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

javascript中的key:value這種東西該怎么處理

javascript中的key:value這種東西該怎么處理

隔江千里 2018-11-30 14:11:12
最近遇到了一個算法題,要求對一個key:value這樣的數(shù)組,根據value值對對key進行排序(這里的value值可以指多行),一個酒店的評分系統(tǒng)的邏輯。名稱 衛(wèi)生 用戶體驗 安全性A x1 y1 z1B x2 y2 z2... ... ... ... 類似于上面的這樣子的,然后先對衛(wèi)生排序,衛(wèi)生排完,選出衛(wèi)生排序得出的前三名,選擇前面選出的前三民,根據用戶體驗排序,選出用戶體驗前兩名,根據安全性排序,選出安全性的第一名。最后輸出這個第一名。感覺其實都差不多,但是資料也查過,map函數(shù)也看了,但是還是理解不了到底該怎么搞,求大佬賜教一波。(ps:明明感覺自己算法算不上差啊,可是每次遇到稍微復雜一點的算法就被搞暈了,剛入前端坑不久,基本的js相關的代碼都擼過一遍),求大佬解惑。
查看完整描述

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 }


查看完整回答
反對 回復 2018-12-07
  • 1 回答
  • 0 關注
  • 1432 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號