Javascript 从对象中获取具有最高值的键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27376295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting key with the highest value from object
提问by Tomek Buszewski
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:)?我尝试将其转换为数组,然后进行排序,但没有成功,因为它是按字母顺序排序的(为了从三个值中获取一个值而来回处理数据似乎有点矫枉过正)。
回答by CD..
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:
在ES6 中:
var obj = {a: 1, b: 2, undefined: 1};
Object.keys(obj).reduce((a, b) => obj[a] > obj[b] ? a : b);
回答by Faris Zacina
Using Underscore or Lo-Dash:
使用下划线或 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]);
回答by polyccon
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
这将返回一个数组,其中所有键的键都具有最大值,以防某些键具有相等的值。例如:如果
const obj = {apples: 1, bananas: 1, pears: 1 }
//This will return ['apples', 'bananas', 'pears']
const obj = {apples: 1,bananas: 1, pears: 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 = {苹果:1,香蕉:2,梨:1};//这将返回 ['bananas']
---> 从数组中取出字符串: ['bananas'][0] //returns 'bananas'`
回答by Amit Joki
Supposing you've an Objectlike this:
假设你有Object这样的:
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]);
回答by astroanu
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));
回答by Charley Bodkin
const data = {
abc: '',
myShortKey: '',
myVeryLongKey: ''
}
const longestKeyLength = Math.max(...Object.keys(data).map((d) => d.length));
console.log(longestKeyLength);
console.log(Object.entries(data).map(([key, val]) => `${key}: ${key.length}`));

