admin管理员组文章数量:1181439
Please consider these Key-Value Pairs:
var dict_Numbers = {"96": "0",
"97": "1",
"98": "2",
"99": "1",
"100": "4",
"101": "0"}
I would like to get the highest value - in this example it would be 101.
How can I achieve this?
Thanks
Update 1:
I use this code: Fast way to get the min/max values among properties of object and Getting key with the highest value from object
but both return Max Value from string comparator
Please consider these Key-Value Pairs:
var dict_Numbers = {"96": "0",
"97": "1",
"98": "2",
"99": "1",
"100": "4",
"101": "0"}
I would like to get the highest value - in this example it would be 101.
How can I achieve this?
Thanks
Update 1:
I use this code: Fast way to get the min/max values among properties of object and Getting key with the highest value from object
but both return Max Value from string comparator
Share Improve this question edited Jul 5, 2021 at 17:48 Vlad L 1,6853 gold badges9 silver badges22 bronze badges asked Dec 28, 2016 at 8:37 DooDooDooDoo 13.4k70 gold badges189 silver badges316 bronze badges 5 |5 Answers
Reset to default 14Nice example from MDN:
var dict_Numbers = {"96": "0",
"97": "1",
"98": "2",
"99": "3",
"100": "4",
"101": "5"}
function getMax(obj) {
return Math.max.apply(null,Object.keys(obj));
}
console.log(getMax(dict_Numbers));
Applying to the keys the easily found Getting key with the highest value from object paying attention to the strings
const dict_Numbers = {
"96": "0",
"97": "1",
"08": "8", // just to make sure
"09": "9", // just to make sure
"98": "2",
"99": "3",
"100": "4",
"101": "5"
},
max = Object.keys(dict_Numbers)
.reduce((a, b) => +a > +b ? +a : +b)
console.log(max)
But as I commented on the question, there is a neater way using Math.max on the Object.keys
Now even more elegant using spread
const dict_Numbers = {
"96": "0",
"97": "1",
"08": "8", // just to make sure
"09": "9", // just to make sure
"98": "2",
"99": "3",
"100": "4",
"101": "5"
},
max = Math.max(...Object.keys(dict_Numbers))
console.log(max)
var dict_Numbers = {"96": "0",
"97": "1",
"98": "2",
"99": "3",
"100": "4",
"101": "5"}
console.log(Math.max(...Object.keys(dict_Numbers)));
Note that this code uses ES6 features.
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);
var dict_Numbers = {
"96": "0",
"97": "1",
"98": "2",
"99": "3",
"100": "4",
"101": "5"
},
key,
intKey,
maxKey = 0;
for (key in dict_Numbers) {
intKey = parseInt(key);
if (intKey > maxKey) {
maxKey = intKey;
}
}
console.log(maxKey);
本文标签: Get Max Key in KeyValue Pair in JavaScriptStack Overflow
版权声明:本文标题:Get Max Key in Key-Value Pair in JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738244104a2071056.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
parseInt
to convert the string to an integer. – Arun Kumar Mohan Commented Dec 28, 2016 at 8:42