Search code examples
javascript

get key from a javascript object with minimum value


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.


Solution

  • You may try this:

    var xx={"1632":45,"1856":12,"1848":56,"1548":34,"1843":88,"1451":55,"4518":98,"1818":23,"3458":45,"1332":634,"4434":33};
    
    
    var z=_.keys(_.pick(xx, function(value, key, object) {
    return (value==_.min(_.values(xx)));  
    }))[0];
    
    document.getElementById("my_div").innerHTML=z;
    <script src="http://underscorejs.org/underscore-min.js"></script>
    
    <div id="my_div"> </div>

    A 3rd party library underscore.js has been used. you should try it:

    http://underscorejs.org/underscore-min.js