从对象获取具有最高值的键

时间:2022-04-28 20:49:11

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) from it? I tried converting it to array and then sorting, but it didn't work out, since it got sorted alphabetically (and it seems like a overkill to juggle data back and forth just for getting one value out of three).

如何从中快速提取最大值标识符(此处为:b)?我尝试将它转换为数组,然后进行排序,但它没有成功,因为它按字母顺序排序(并且为了从三个中获取一个值而来回看待来回数据似乎是一种过度杀伤力)。

5 个解决方案

#1


61  

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

#2


15  

Using Underscore or Lo-Dash:

使用Underscore或Lo-Dash:

var maxKey = _.max(Object.keys(obj), function (o) { return obj[o]; });

With ES6 Arrow Functions:

使用ES6箭头功能:

var maxKey = _.max(Object.keys(obj), o => obj[o]);

jsFiddle demo

#3


4  

Supposing you've an Object like this:

假设你有一个像这样的对象:

var obj = {a: 1, b: 2, undefined: 1}

You can do this

你可以这样做

var max = Math.max.apply(null,Object.keys(obj).map(function(x){ return obj[x] }));
console.log(Object.keys(obj).filter(function(x){ return obj[x] == max; })[0]);

#4


3  

Here is a suggestion in case you have many equal values and not only one maximum:

如果您有许多相等的值而不仅仅是一个最大值,这是一个建议:

    const getMax = object => {
        return Object.keys(object).filter(x => {
             return object[x] == Math.max.apply(null, 
             Object.values(object));
       });
    };

This returns an array, with the keys for all of them with the maximum value, in case there are some that have equal values. For example: if

这将返回一个数组,其中所有数组的键都具有最大值,以防某些数组具有相同的值。例如:if

const obj = {apples: 1, bananas: 1, pears: 1 }
//This will return ['apples', 'bananas', 'pears']

const obj = {苹果:1,香蕉:1,梨:1} //这会返回['apples','bananas','pears']

If on the other hand there is a maximum:

如果另一方面有最大值:

const obj = {apples: 1, bananas: 2, pears: 1 }; //This will return ['bananas']
---> To get the string out of the array: ['bananas'][0] //returns 'bananas'`

const obj = {apples:1,香蕉:2,梨:1}; //这将返回['bananas'] --->从数组中取出字符串:['bananas'] [0] //返回'香蕉'

#5


2  

Very basic method. might be slow to process

非常基本的方法。可能进展缓慢

var v = {a: 1, b: 2, undefined: 1};

function geth(o){
    var vals = [];    
    for(var i in o){
       vals.push(o[i]);
    }

    var max = Math.max.apply(null, vals);

     for(var i in o){
        if(o[i] == max){
            return i;
        }
    }
}

console.log(geth(v));

#1


61  

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

#2


15  

Using Underscore or Lo-Dash:

使用Underscore或Lo-Dash:

var maxKey = _.max(Object.keys(obj), function (o) { return obj[o]; });

With ES6 Arrow Functions:

使用ES6箭头功能:

var maxKey = _.max(Object.keys(obj), o => obj[o]);

jsFiddle demo

#3


4  

Supposing you've an Object like this:

假设你有一个像这样的对象:

var obj = {a: 1, b: 2, undefined: 1}

You can do this

你可以这样做

var max = Math.max.apply(null,Object.keys(obj).map(function(x){ return obj[x] }));
console.log(Object.keys(obj).filter(function(x){ return obj[x] == max; })[0]);

#4


3  

Here is a suggestion in case you have many equal values and not only one maximum:

如果您有许多相等的值而不仅仅是一个最大值,这是一个建议:

    const getMax = object => {
        return Object.keys(object).filter(x => {
             return object[x] == Math.max.apply(null, 
             Object.values(object));
       });
    };

This returns an array, with the keys for all of them with the maximum value, in case there are some that have equal values. For example: if

这将返回一个数组,其中所有数组的键都具有最大值,以防某些数组具有相同的值。例如:if

const obj = {apples: 1, bananas: 1, pears: 1 }
//This will return ['apples', 'bananas', 'pears']

const obj = {苹果:1,香蕉:1,梨:1} //这会返回['apples','bananas','pears']

If on the other hand there is a maximum:

如果另一方面有最大值:

const obj = {apples: 1, bananas: 2, pears: 1 }; //This will return ['bananas']
---> To get the string out of the array: ['bananas'][0] //returns 'bananas'`

const obj = {apples:1,香蕉:2,梨:1}; //这将返回['bananas'] --->从数组中取出字符串:['bananas'] [0] //返回'香蕉'

#5


2  

Very basic method. might be slow to process

非常基本的方法。可能进展缓慢

var v = {a: 1, b: 2, undefined: 1};

function geth(o){
    var vals = [];    
    for(var i in o){
       vals.push(o[i]);
    }

    var max = Math.max.apply(null, vals);

     for(var i in o){
        if(o[i] == max){
            return i;
        }
    }
}

console.log(geth(v));