Getting key with the highest value from object
For example:
var obj = {a: 1, b: 2, undefined: 1};
Object.keys(obj).reduce(function(a, b){ return obj[a] > obj[b] ? a : b });
In ES6:
var obj = {a: 1, b: 2, undefined: 1};
Object.keys(obj).reduce((a, b) => obj[a] > obj[b] ? a : b);
Get Max Key in Key-Value Pair in JavaScript
Try this.
You can iterate over the properties of the object and check for its value.
var dict_Numbers = { "96": "0", "97": "1", "98": "2", "99": "3", "100": "4", "101": "5"};
var max = 0;
for (var property in dict_Numbers) { max = (max < parseFloat(property)) ? parseFloat(property) : max;}
console.log(max);
Get object keys with the highest value in Javascript
As commented before:
- Create an array of keys:
Object.keys(object)
- Sort this array based on value:
sort((a,b)=> object[b] - object[a])
- Get necessary values:
keys.slice(0,n)
var value = {2:1,53:2,56:4,57:9,61:2,62:16,63:2,398:24};
function getKeysWithHighestValue(o, n){ var keys = Object.keys(o); keys.sort(function(a,b){ return o[b] - o[a]; }) console.log(keys); return keys.slice(0,n);}
console.log(getKeysWithHighestValue(value, 4))
Getting key with maximum value in dictionary?
You can use operator.itemgetter
for that:
import operator
stats = {'a': 1000, 'b': 3000, 'c': 100}
max(stats.iteritems(), key=operator.itemgetter(1))[0]
And instead of building a new list in memory use stats.iteritems()
. The key
parameter to the max()
function is a function that computes a key that is used to determine how to rank items.
Please note that if you were to have another key-value pair 'd': 3000 that this method will only return one of the two even though they both have the maximum value.
>>> import operator
>>> stats = {'a': 1000, 'b': 3000, 'c': 100, 'd': 3000}
>>> max(stats.iteritems(), key=operator.itemgetter(1))[0]
'b'
If using Python3:
>>> max(stats.items(), key=operator.itemgetter(1))[0]
'b'
Getting keys with maximum value in JavaScript hashmap/object
You could first calculate the max value as a separate operation and then just filter:
const hash = {Apple: 2, Orange: 1, Mango: 2};
const max = Object.keys(hash).reduce((a, v) => Math.max(a, hash[v]), -Infinity);const result = Object.keys(hash).filter(v => hash[v] === max);
console.log(result);
find highest key with value from Object
Easy. Sort Object.entries on the value, then return the first "entry"
The result will be in the form ["key", value]
let dates = {
'2021-08-06': 39,
'2021-08-07': 0,
'2021-08-08': 0,
'2021-08-09': 149,
'2021-08-10': 174,
'2021-08-11': 231,
'2021-08-12': 300
}
const result = Object.entries(dates).sort(([, a], [, b]) => b - a)[0];
console.log(result);
Related Topics
How to Animate Scrolltop with Jquery
JavaScript Use Variable as Object Name
Angularjs Multiple Filter with Custom Filter Function
Is There a Sleep Function in JavaScript
Handle Url Fragment Identifier (Anchor) Change Event in JavaScript
How to Pass Parameters to a Script Tag
Validate That a String Is a Positive Integer
How to Give Keyboard Focus to a Div and Attach Keyboard Event Handlers to It
Plain Count Up Timer in JavaScript
Download File from Bytes in JavaScript
How to Define Setter/Getter on Prototype
Sum of Two Numbers with Prompt
JavaScript Regular Expressions and Sub-Matches
Calling a JSON API with Node.Js
Add Onclick Event to Newly Added Element in JavaScript