I am trying to get key from javascript object having a minium value.
var myobj = {"1632":45,"1856":12,"1848":56,"1548":34,"1843":88,"1451":55,"4518":98,"1818":23,"3458":45,"1332":634,"4434":33};
i have to get the key which having minimum value. i.e:
1856
trying hard to get. i am new with object manipulation.
Short and Sweet :
let key = Object.keys(obj).reduce((key, v) => obj[v] < obj[key] ? v : key);
Iterate over the object properties and get key based on min value.
var myjson = {
"1632": 45,
"1856": 12,
"1848": 56,
"1548": 34,
"1843": 88,
"1451": 55,
"4518": 98,
"1818": 23,
"3458": 45,
"1332": 634,
"4434": 33
};
// get object keys array
var keys = Object.keys(myjson),
// set initial value as first elemnt in array
res = keys[0];
// iterate over array elements
keys.forEach(function(v) {
// compare with current property value and update with the min value property
res = +myjson[res] > +myjson[v] ? v : res;
});
console.log(res);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With