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

时间:2014-12-09 10:09:14

标签: javascript

我有一个像这样的对象:

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

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

8 个答案:

答案 0 :(得分:83)

例如:

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

Object.keys(obj).reduce(function(a, b){ return obj[a] > obj[b] ? a : b });

ES6

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

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

答案 1 :(得分:22)

使用Underscore或Lo-Dash:

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

使用ES6箭头功能:

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

jsFiddle demo

答案 2 :(得分:7)

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

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

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

  

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

如果另一方面有最大值:

  

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

答案 3 :(得分:4)

假设你有Object这样:

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

你可以这样做

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 :(得分:2)

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

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

答案 5 :(得分:1)

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

我见过的最好的工作是这个

const chars = {a: 1, b: 2, undefined: 1}

//set maximum value to 0 and maxKey to an empty string
let max = 0;
let maxKey = "";

for(let char in chars){
  if(chars[char]> max){
    max = chars[char];
    maxKey= char
  }
}

console.log(maxKey)

答案 6 :(得分:0)

#include <iostream>
#include<stdlib.h>

#include <string>


using namespace std;

// global variable
string  plantType;
int temperatures ;
string water; 
string sunExposure; 
bool pet;
string plant1,plant2,plant3;

class inside{
public:

void low(){
    string plant1 [3]={"Snake plant","Spider plant","Aloe Vera plant"};
    for(int i =0; i<3; i++){
        cout << "* "<< plant1[i]<<endl;
    }

}
void medium(){
    string plant2 [5]={"Pothos plant","Dracaena plant","ZZ plant","Rubber plant","Philodendron Green plant"};
  for(int i =0; i<5; i++){
        cout << "* "<< plant2[i]<<endl;

}
}
void high(){
    string plant3 [2]={"Bird’s Nest Fern plant","Peace Lily plant"};
     for(int i =0; i<2; i++){
        cout << "* "<< plant3[i]<<endl;

}
}

void pick(){
    cout <<"Best choice for you is: ";



    if (temperatures >= 13 && temperatures <=29 ){

        if(water=="low"){


            if(sunExposure=="fully"){
             cout<<"test"<<endl;
                if(pet==true){
                    cout<<plant1[1]<<endl; //this line cannot be executed 
                  
                }
            }
        }}}

int main(){
cout <<"Where do you want to grow the plant (inside), or (outside)"<<endl;
        cin>>grow;



        if (grow == "inside"){
                //inside home



     cout<<endl<<"inside"<<endl;
     cout<<"Enter the Temperature in (Celsius scale 13-29)"<<endl;
     cin>>temperatures;
     cout<<"Enter water level (low - medium - high)"<<endl;
     cin>>water;
     cout<<"Enter Sun Exposure (fully - partly - shady)"<<endl;
     cin>>sunExposure;
     cout<<"Do you have pet (true or false)? "<<endl;
     cin>>pet;


        inside inside;
        inside.pick();
        }
}

答案 7 :(得分:0)

let data = {a:1,b:2,undefined:3}
let maxValue = Object.entries(data).sort((x,y)=>y[1]-x[1])[0]

注意:这是一个非常昂贵的过程,如果与大尺寸(>=1000000)的对象一起使用,则会阻塞事件循环。使用大数组切片条目并使用 setTimeout 循环调用上述方法。