最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

get key from a javascript object with minimum value - Stack Overflow

programmeradmin7浏览0评论

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.

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.

Share Improve this question edited Jun 24, 2016 at 7:58 deceze 522k88 gold badges798 silver badges939 bronze badges asked Jun 24, 2016 at 7:51 Deepak SharmaDeepak Sharma 1,1371 gold badge12 silver badges23 bronze badges 1
  • Yes , sorry for mention. have update the answer. – Deepak Sharma Commented Jun 24, 2016 at 7:57
Add a ment  | 

6 Answers 6

Reset to default 14

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) {
  // pare with current property value and update with the min value property
  res = +myjson[res] > +myjson[v] ? v : res;
});

console.log(res);

You could use Array.reduce()

var object = { "1632": 45, "1856": 12, "1848": 56, "1548": 34, "1843": 88, "1451": 55, "4518": 98, "1818": 23, "3458": 45, "1332": 634, "4434": 33 },
    key = Object.keys(object).reduce(function (r, a, i) {
        return !i || +object[a] < +object[r] ? a : r;
    }, undefined);

console.log(key);

Learner's approach by for-looping:

var myobj = {"1632":45,"1856":12,"1848":56,"1548":34,"1843":88,"1451":55,"4518":98,"1818":23,"3458":45,"1332":634,"4434":33};

// Get the keys of myobj so we can iterate through it
var keys = Object.keys(myobj);

// Iterate through all the key values
var minimumKey = keys[0];
for(var i = 1; i < keys.length; i++){
    var minimum = myobj[minimumKey];
    var value = myobj[keys[i]];
    if(minimum > value) minimumKey = keys[i];
}

console.log(minimumKey, myobj[minimumKey]);

A more functional approach:

var myobj = {"1632":45,"1856":12,"1848":56,"1548":34,"1843":88,"1451":55,"4518":98,"1818":23,"3458":45,"1332":634,"4434":33};

var minimum = Object.keys(myobj).map(function(key){
    return {
        "key": key,
        "value": myobj[key]
    }
}).sort(function(a, b){
    return a.value - b.value
})[0];

console.log(minimum);
console.log(minimum.key);
console.log(minimum.value);

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/underscore-min.js"></script>

<div id="my_div"> </div>

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

http://underscorejs/underscore-min.js

What I did, was iterate over the map, and if the value is lower than your current answer, then I overwrite the answer (100000 was the max value possible):

let answer = [100000, 100000]

const mapIterator = mapObject[Symbol.iterator]();

for (const item of mapIterator) {
  if (item[1] < answer[1]) answer = item
}
console.log(answer);

Map iterator docs: https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/@@iterator

发布评论

评论列表(0)

  1. 暂无评论