4

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.

1
  • Yes , sorry for mention. have update the answer. Commented Jun 24, 2016 at 7:57

6 Answers 6

14

Short and Sweet :

let key = Object.keys(obj).reduce((key, v) => obj[v] < obj[key] ? v : key);
Sign up to request clarification or add additional context in comments.

Comments

2

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);

Comments

2

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);

2 Comments

Whats significance of !i just after the return
@Sumer, it prevents to check two values, if the key count is one. in this case, ther e is no other key to compare the object with.
1

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);

Comments

0

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

1 Comment

Not enough jQuery! (Meaning: why do you need a 3rd party library for such a simple task...?)
0

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.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/@@iterator

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.