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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

返回對(duì)象數(shù)組中具有最大值的鍵的最簡(jiǎn)單方法是什么?

返回對(duì)象數(shù)組中具有最大值的鍵的最簡(jiǎn)單方法是什么?

Qyouu 2021-11-12 18:24:01
我正在制作一個(gè)簡(jiǎn)單的 RPG 并嘗試計(jì)算當(dāng)角色升級(jí)時(shí)應(yīng)該增加哪個(gè)屬性。他們對(duì)每個(gè)屬性都有一個(gè)潛在的限制,我想增加離其潛力最遠(yuǎn)的屬性。我可以遍歷每個(gè)屬性并從其潛在值中減去其當(dāng)前值以獲得差異。然后我可以將差異推送到數(shù)組。結(jié)果如下:[{Strength: 5},{Dexterity: 6},{Constitution: 3},{Wisdom: 4},{Charisma: 8}]魅力是差異最大的鍵,那么我如何評(píng)估它并返回鍵的名稱(而不是值本身)?編輯:這是用于獲取數(shù)組的邏輯:let difference = [];let key;for (key in currentAttributes) {  difference.push({[key]: potentialAttributes[key] - currentAttributes[key]});};
查看完整描述

3 回答

?
30秒到達(dá)戰(zhàn)場(chǎng)

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超6個(gè)贊

使用 Object.entries 進(jìn)行簡(jiǎn)單的 reduce


const items = [

  { Strength: 5 },

  { Dexterity: 6 },

  { Constitution: 3 },

  { Wisdom: 4 },

  { Charisma: 8 }

]


const biggest = items.reduce((biggest, current, ind) => {

  const parts = Object.entries(current)[0]  //RETURNS [KEY, VALUE]

  return (!ind || parts[1] > biggest[1]) ? parts : biggest  // IF FIRST OR BIGGER

}, null) 

console.log(biggest[0])  // 0 = KEY, 1 = BIGGEST VALUE


您的數(shù)據(jù)模型對(duì)于帶有對(duì)象的數(shù)組有點(diǎn)奇怪,更好的模型只是一個(gè)對(duì)象。


const items = {

  Strength: 5,

  Dexterity: 6,

  Constitution: 3,

  Wisdom: 4,

  Charisma: 8

}


const biggest = Object.entries(items)

  .reduce((biggest, current, ind) => {

    const parts = current

    return (!ind || parts[1] > biggest[1]) ? parts : biggest  

}, null) 


console.log(biggest[0])


查看完整回答
反對(duì) 回復(fù) 2021-11-12
?
12345678_0001

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超5個(gè)贊

您可以創(chuàng)建一個(gè)對(duì)象,獲取條目并通過獲取具有最大值的條目來減少條目。最后從入口拿鑰匙。


var data = [{ Strength: 5 }, { Dexterity: 6 }, { Constitution: 3 }, { Wisdom: 4 }, { Charisma: 8 }],

    greatest = Object

        .entries(Object.assign({}, ...data))

        .reduce((a, b) => a[1] > b[1] ? a : b)

        [0];


console.log(greatest);


查看完整回答
反對(duì) 回復(fù) 2021-11-12
?
SMILET

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超4個(gè)贊

按降序排序并獲取第一項(xiàng):


let attributes = [

  {Strength: 5},

  {Dexterity: 6},

  {Constitution: 3},

  {Wisdom: 4},

  {Charisma: 8}

];


//for convenience

const getValue = obj => Object.values(obj)[0];


//sort descending

attributes.sort((a, b) => getValue(b) - getValue(a));


let highest = attributes[0];

console.log(Object.keys(highest)[0]);

或者,遍歷數(shù)組并找到最高分:


let attributes = [

  {Strength: 5},

  {Dexterity: 6},

  {Constitution: 3},

  {Wisdom: 4},

  {Charisma: 8}

];


//for convenience

const getValue = obj => Object.values(obj)[0];


//find the highest score

let highest = attributes.reduce((currentHighest, nextItem) => getValue(currentHighest) > getValue(nextItem) ?  currentHighest : nextItem);


console.log(Object.keys(highest)[0]);


查看完整回答
反對(duì) 回復(fù) 2021-11-12
  • 3 回答
  • 0 關(guān)注
  • 222 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)