Getting key with the highest value from object

后端 未结 6 2029
闹比i
闹比i 2020-11-27 02:51

I have a object like that one:

Object {a: 1, b: 2, undefined: 1} 

How can I quickly pull the largest value identifier (here: b

6条回答
  •  [愿得一人]
    2020-11-27 03:27

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

提交回复
热议问题