Skip to content

Instantly share code, notes, and snippets.

@tanaikech
Created June 14, 2018 23:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tanaikech/fcbc0204e92d8f1cdde74dc5ac820753 to your computer and use it in GitHub Desktop.
Save tanaikech/fcbc0204e92d8f1cdde74dc5ac820753 to your computer and use it in GitHub Desktop.
Retrieving a Key with Maximum Value from Object

Retrieving a Key with Maximum Value from Object

This sample script is for retrieving a key with the maximum value from an object. This can be also used by Google Apps Script.

var obj = {"a": 5, "b": 4, "c": 3, "d": 2, "e": 1};
var res = Object.keys(obj).reduce(function(a, c) {
    return obj[a] > obj[c] ? a : c;
});

Logger.log(res); // >>> a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment