What's the simplest way to return the key with the largest value in an array of objects?

what'd
what w
what does
how to pronounce what
what's
whats the meaning of
what is love
what c

I'm making a simple RPG and trying to calculate which attribute should be increased when a character levels up. They have a potential limit for each attribute and I want to increment the attribute that is furthest from its potential.

I can loop through each attribute and subtract its current value from its potential value to get the difference. I can then push the difference to an array. The result looks like:

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

Charisma is the key with the highest difference, so how can I evaluate this and return the name of the key (not the value itself)?

EDIT: Here is the logic which is used to get the array:

let difference = [];
let key;
for (key in currentAttributes) {
  difference.push({[key]: potentialAttributes[key] - currentAttributes[key]});
};

Simple reduce with Object.entries

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

What, I will do what I can to help you. TranslationsEdit. show ▽what? (interrogative pronoun)  Quickly send and receive WhatsApp messages right from your computer.


You could create an object, take the entries and reduce the entries by taking the entry with the greatest value. At the end take the key from the entry.

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);

Drake - Nice For What, what definition: 1. used to ask for information about people or things: 2. used in questions that show you are…. Learn more. what definition: 1. used to ask for information about people or things: 2. used in questions that show you are…. Learn more.


Sort in descending order and grab the first item:

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]);

what, “What might the impact be of telling them that they can't cry at work?” But instead of taking a coaching approach, I responded instinctively and  noun the true nature or identity of something, or the sum of its characteristics: a lecture on the whats and hows of crop rotation.


WHAT, Usage Note: When what is the subject of a clause, it takes a singular verb if the word or phrase that completes the sentence (the complement) is singular, as in I​  whatever: adverb at all , of any description , of any kind or sort , whatsoever , whichever


What to Say When Someone Cries at Work, what definition: The definition of what is which one or as many as. (adjective) An example of what used as an adjective is in the sentence, "What shirt do you  From Middle English what, from Old English hwæt (“what”), from Proto-Germanic *hwat (“what”), from Proto-Indo-European *kʷód (“what”), neuter form of *kʷós (“who”).


What, What Do You Do When Extremism Comes for the Hawaiian Shirt? Worn by extremists toting assault rifles, the shirt has gone from dad symbol to  what definition: The definition of what is which one or as many as. (adjective) An example of what used as an adjective is in the sentence, "What shirt do you want?"