-1

Im trying to get the user & value with the highest number from this array but have had no luck in my searches. I'm starting to wonder if my array is poorly written.

{
  "radtech2": 1,
  "conorlarkin4": 25,
  "jdon2001": 15,
  "nobel_veo": 101,
  "frapoden": 1,
  "duckyboy17": 31,
  "faeded": 30,
  "jimbob20001": 17,
  "leb0wski": 15,
  "3cavalry": 2,
  "hardoak22": 25,
  "deep_slide": 10000,
  "sillywil": 7
}
  • 3
    To start with - this is not an array - it is an object. – gavgrif Apr 29 '19 at 04:15
  • 3
    I did not downvote you - and I also did not provide an alternative answer because I agree with the comment from @CertainPerformance - in which you have not attempted to show any workings or attempted solution. Simply giving an answer to the question does not address that issue nor does it inspire you to try something and ask for help. SO is not a code writing service - it is a code assistance service. That presumes the presence of code. – gavgrif Apr 29 '19 at 04:18
  • Thanks, i'm not an avid stackoverflow user so it was not my intention to "do it wrong". I've tried several things to solve my problem already & did not know I had to post them here in order to have the honour of getting an answer. –  Apr 29 '19 at 04:23
  • 2
    The first comment should have been a guide on what to do to meet the requirements of a good post. – gavgrif Apr 29 '19 at 04:24
  • What do you expect the result to look like? The title says you want the value *and* username, but the question text makes it sound like you just want the username. – Herohtar Apr 29 '19 at 04:27
  • 1
    Also, probably a duplicate of [Getting key with the highest value from object](https://stackoverflow.com/questions/27376295/getting-key-with-the-highest-value-from-object) – Herohtar Apr 29 '19 at 04:30
  • @herohtar Thanks for pointing that out, I have edited the question. –  Apr 29 '19 at 04:30

4 Answers4

0

const users = {
  "radtech2": 1,
  "conorlarkin4": 25,
  "jdon2001": 15,
  "nobel_veo": 101,
  "frapoden": 1,
  "duckyboy17": 31,
  "faeded": 30,
  "jimbob20001": 17,
  "leb0wski": 15,
  "3cavalry": 2,
  "hardoak22": 25,
  "deep_slide": 10000,
  "sillywil": 7
};

const highestUser = users => Object.keys(users).reduce(
  (highest, current) => highest.val > users[current] ? highest : { user: current, val: users[current] },
  { user: undefined, val: -Infinity }
).user;

console.log(highestUser(users));
Adrian Brand
  • 20,384
  • 4
  • 39
  • 60
0

Use keys() and entries() methods to search your JSON object. Save largest value into e.g. const largest and then find out which key belongs to this value.

Martin Spd
  • 53
  • 1
  • 4
-1

Let me try to squeeze it into a one-liner approach using Object.keys() and Array.reduce().

const users = {
  "radtech2": 1,
  "conorlarkin4": 25,
  "jdon2001": 15,
  "nobel_veo": 101,
  "frapoden": 1,
  "duckyboy17": 31,
  "faeded": 30,
  "jimbob20001": 17,
  "leb0wski": 15,
  "3cavalry": 2,
  "hardoak22": 25,
  "deep_slide": 10000,
  "sillywil": 7
};


const res = Object.keys(users).reduce((a, b) => users[a] > users[b] ? a : b);

console.log(res);

How the above code works is that I get the array of keys from the users object, and I use reduce to get the highest possible value and return the corresponding property from the array obtained from Object.keys().

wentjun
  • 40,384
  • 10
  • 95
  • 107
-1

What you show in your question is an Object, not an Array; however, it does need to be turned into an array in order to work with it.

You can do that with Object.entries(), which will return an array of all the key/value pairs in the object.

Then you can use Array.reduce() to extract the one with the largest value.

const data = {
  "radtech2": 1,
  "conorlarkin4": 25,
  "jdon2001": 15,
  "nobel_veo": 101,
  "frapoden": 1,
  "duckyboy17": 31,
  "faeded": 30,
  "jimbob20001": 17,
  "leb0wski": 15,
  "3cavalry": 2,
  "hardoak22": 25,
  "deep_slide": 10000,
  "sillywil": 7
}

let winner = Object.entries(data).reduce((a, b) => (a[1] > b[1]) ? a : b)

console.log(winner)
Herohtar
  • 5,347
  • 4
  • 31
  • 41