Getting the lowest value with key in array












11















I have an array like this: arr = {lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49, …}



And I'm trying get the lowest value with key using Javascript.



What I've tried:



alert(Math.min.apply(Math, arr)); returns Infinity I don't know why



I got this on Google, just for try:



var keys = Object.keys(arr).map(Number).filter(function(a){
return arr[a];
}); alert(Math.min.apply(Math, keys));


returns Infinity too



I want something more complete, like this output: "The lowest value is 2 from lst9".



I really tried fix it myself before asking here, but without success!
Can you help me fix this "Infinity" issue? Thank you.










share|improve this question









New contributor




Mernt is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















  • What is expected output?

    – Maheer Ali
    Mar 25 at 6:45






  • 2





    Do you want to get, for example, lst3 if obj.lst3 is the lowest value?

    – Jack Bashford
    Mar 25 at 6:47






  • 2





    Also, what is lst9? Is it lst9: 2?

    – Jack Bashford
    Mar 25 at 6:48











  • may be a duplicte of stackoverflow.com/questions/38008307/…

    – Syed Mehtab Hassan
    Mar 25 at 6:49








  • 9





    arr is not an array but an object

    – Isaac
    Mar 25 at 6:53
















11















I have an array like this: arr = {lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49, …}



And I'm trying get the lowest value with key using Javascript.



What I've tried:



alert(Math.min.apply(Math, arr)); returns Infinity I don't know why



I got this on Google, just for try:



var keys = Object.keys(arr).map(Number).filter(function(a){
return arr[a];
}); alert(Math.min.apply(Math, keys));


returns Infinity too



I want something more complete, like this output: "The lowest value is 2 from lst9".



I really tried fix it myself before asking here, but without success!
Can you help me fix this "Infinity" issue? Thank you.










share|improve this question









New contributor




Mernt is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















  • What is expected output?

    – Maheer Ali
    Mar 25 at 6:45






  • 2





    Do you want to get, for example, lst3 if obj.lst3 is the lowest value?

    – Jack Bashford
    Mar 25 at 6:47






  • 2





    Also, what is lst9? Is it lst9: 2?

    – Jack Bashford
    Mar 25 at 6:48











  • may be a duplicte of stackoverflow.com/questions/38008307/…

    – Syed Mehtab Hassan
    Mar 25 at 6:49








  • 9





    arr is not an array but an object

    – Isaac
    Mar 25 at 6:53














11












11








11


1






I have an array like this: arr = {lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49, …}



And I'm trying get the lowest value with key using Javascript.



What I've tried:



alert(Math.min.apply(Math, arr)); returns Infinity I don't know why



I got this on Google, just for try:



var keys = Object.keys(arr).map(Number).filter(function(a){
return arr[a];
}); alert(Math.min.apply(Math, keys));


returns Infinity too



I want something more complete, like this output: "The lowest value is 2 from lst9".



I really tried fix it myself before asking here, but without success!
Can you help me fix this "Infinity" issue? Thank you.










share|improve this question









New contributor




Mernt is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












I have an array like this: arr = {lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49, …}



And I'm trying get the lowest value with key using Javascript.



What I've tried:



alert(Math.min.apply(Math, arr)); returns Infinity I don't know why



I got this on Google, just for try:



var keys = Object.keys(arr).map(Number).filter(function(a){
return arr[a];
}); alert(Math.min.apply(Math, keys));


returns Infinity too



I want something more complete, like this output: "The lowest value is 2 from lst9".



I really tried fix it myself before asking here, but without success!
Can you help me fix this "Infinity" issue? Thank you.







javascript arrays object






share|improve this question









New contributor




Mernt is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Mernt is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited Mar 25 at 20:38









Jack Bashford

13.7k31848




13.7k31848






New contributor




Mernt is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Mar 25 at 6:44









MerntMernt

645




645




New contributor




Mernt is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Mernt is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Mernt is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.













  • What is expected output?

    – Maheer Ali
    Mar 25 at 6:45






  • 2





    Do you want to get, for example, lst3 if obj.lst3 is the lowest value?

    – Jack Bashford
    Mar 25 at 6:47






  • 2





    Also, what is lst9? Is it lst9: 2?

    – Jack Bashford
    Mar 25 at 6:48











  • may be a duplicte of stackoverflow.com/questions/38008307/…

    – Syed Mehtab Hassan
    Mar 25 at 6:49








  • 9





    arr is not an array but an object

    – Isaac
    Mar 25 at 6:53



















  • What is expected output?

    – Maheer Ali
    Mar 25 at 6:45






  • 2





    Do you want to get, for example, lst3 if obj.lst3 is the lowest value?

    – Jack Bashford
    Mar 25 at 6:47






  • 2





    Also, what is lst9? Is it lst9: 2?

    – Jack Bashford
    Mar 25 at 6:48











  • may be a duplicte of stackoverflow.com/questions/38008307/…

    – Syed Mehtab Hassan
    Mar 25 at 6:49








  • 9





    arr is not an array but an object

    – Isaac
    Mar 25 at 6:53

















What is expected output?

– Maheer Ali
Mar 25 at 6:45





What is expected output?

– Maheer Ali
Mar 25 at 6:45




2




2





Do you want to get, for example, lst3 if obj.lst3 is the lowest value?

– Jack Bashford
Mar 25 at 6:47





Do you want to get, for example, lst3 if obj.lst3 is the lowest value?

– Jack Bashford
Mar 25 at 6:47




2




2





Also, what is lst9? Is it lst9: 2?

– Jack Bashford
Mar 25 at 6:48





Also, what is lst9? Is it lst9: 2?

– Jack Bashford
Mar 25 at 6:48













may be a duplicte of stackoverflow.com/questions/38008307/…

– Syed Mehtab Hassan
Mar 25 at 6:49







may be a duplicte of stackoverflow.com/questions/38008307/…

– Syed Mehtab Hassan
Mar 25 at 6:49






9




9





arr is not an array but an object

– Isaac
Mar 25 at 6:53





arr is not an array but an object

– Isaac
Mar 25 at 6:53












9 Answers
9






active

oldest

votes


















11














You can get the key and value using Object.entries:






var arr = {
lst1: 300,
lst2: 381,
lst3: 4,
lst4: 4,
lst5: 49
};

function lowestValueAndKey(obj) {
var lowestItems = Object.entries(obj).sort(([ ,v1], [ ,v2]) => v1 - v2)[0];
return `Lowest value is ${lowestItems[1]}, with a key of ${lowestItems[0]}`;
}

var lowest = lowestValueAndKey(arr);
console.log(lowest);








share|improve this answer
























  • Hello, Are there an way to make this script compatible with Internet Explorer too?

    – Mernt
    Mar 25 at 7:41











  • @Mernt developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…

    – JollyJoker
    Mar 25 at 7:59











  • I tried but I'm getting a syntax error on line var lowestItems = Object.entries(arr).sort(([ ,v1], [ ,v2]) => v1 - v2)[0]

    – Mernt
    Mar 25 at 8:06











  • If you look here developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… you'll see that Internet Explorer does not support Object.entries

    – ChatterOne
    Mar 25 at 8:08






  • 1





    @Mernt IE doesn't support the fat arrow notation. You need to rewrite ([ ,v1], [ ,v2]) => v1 - v2 in old style Javascript

    – JollyJoker
    Mar 25 at 8:12



















8














There are several ways to arrive at what you want. Please see the following methods:






const obj = { lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49 }
const values = Object.values(obj)
const lowest = Math.min.apply(null, values)

// Using Object.keys(), Object.values() and findIndex()
const keys = Object.keys(obj)
const indexOfLowest = values.findIndex(function (x) { return x === lowest })
console.log(`The lowest value is ${lowest} from ${keys[indexOfLowest]}`)



// OR Using Object.keys() and filter()
const key = Object.keys(obj).filter(function (x) { return obj[x] === lowest })[0]
console.log(`The lowest value is ${lowest} from ${key}`)



// OR Using Object.entries() and sort()
const [[lowestKey, lowestVal]] = Object.entries(obj).sort(function ([,valA], [,valB]) { return valA - valB });
console.log(`The lowest value is ${lowestVal} from ${lowestKey}`)








share|improve this answer


























  • It should work as is in IE > = 11

    – John Kennedy
    Mar 25 at 8:02











  • Syntax error on line var key = Object.keys(arr).filter(x => arr[x] === lowest)[0]

    – Mernt
    Mar 25 at 8:08






  • 1





    I'll suggest using a polyfill if it doesn't work in IE. See here: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… I should also inform you that on my IE on Windows 10 it works without needing a polyfill.

    – John Kennedy
    Mar 25 at 8:10








  • 1





    @JohnKennedy I'd be surprised by that, IE doesn't support arrow functions

    – Nick A
    Mar 25 at 14:47



















7














There are many ways to approach your problem, but here is one simple alternative way to do it. Basically, it is a brute force way of doing things, whereby you iterate through every property to check for the value.



const obj = {lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49};

const getSmallest = function (object) {
let smallestValue = Number.MAX_VALUE;
let selectedKey = '';
for (let key in object) {
if (object[key] < smallestValue) {
smallestValue = object[key];
selectedKey = key;
}
};
console.log(selectedKey, smallestValue)
return `The lowest value is ${smallestValue} from ${selectedKey}`;
};

getSmallest(obj);





share|improve this answer

































    3














    To get only the minimum number you can use Math.min(...Object.entries(arr).map(o => o[1])):






    const arr = {lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49};
    const lowestValue = Math.min(...Object.entries(arr).map(o => o[1]));

    console.log(lowestValue);





    And to get the object with the minimum value you can use Object.entries(arr).reduce((a, [k, v]) => a[Object.keys(a)[0]] < v ? a : {[k]: v}, {}):






    const arr = {lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49};
    const lowestObj = Object.entries(arr).reduce((a, [k, v]) => a[Object.keys(a)[0]] < v ? a : {[k]: v}, {});

    console.log(lowestObj);








    share|improve this answer

































      2














      You can try this:



      obj = {lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49, lst9: 2}; // This is an object, That;s ehy you getting infinite

      var arr = Object.keys( obj ).map(function ( key ) { return obj[key]; });

      alert(Math.min.apply( null, arr ));


      Fiddle: https://jsfiddle.net/ozvubmdr/






      share|improve this answer































        2














        Easy, though not optimal, as it traverses the array twice: find the smallest value, then find the key belonging to it.






        arr = {lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49, lst9: 2}
        min = Math.min(...Object.values(arr));
        minkey = Object.keys(arr).find(key => arr[key] = min);
        console.log(`Lowest is ${min} at ${minkey}`);








        share|improve this answer































          2














          You may try this:






          const arr = {lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49, lst9: 2}
          const minVal = Math.min(...Object.values(arr))
          const fromKey = Object.keys(arr).find(key => arr[key] === minVal)
          console.log(`The lowest value is ${minVal} from ${fromKey}`)








          share|improve this answer































            2














            Here we first assume that the min, key and value as first property and the value respectively. Then we iterate through all the elements and compare with minValue to find any value less than that in the object. If it is, then we update both minValue and minKey. Anyway, the arr type is an Object. it is not an array in javascript.



            var arr = {lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49};
            var minkey = Object.keys(arr)[0];
            var minValue = arr[Object.keys(arr)[0]];
            for (var key in arr) {
            if (arr.hasOwnProperty(key)) {
            if(arr[key] < minValue){
            minValue = arr[key];
            minKey = key;
            }
            }
            }





            share|improve this answer































              2














              You can use Object.entries and use Array.reduce to reduce the resulting [key-value] pair to an object which will have lowest entry:






              const data = {lst1: 300, lst2: -381, lst3: 4, lst4: 4, lst5: 49, lst6: -49, lst7: 0, lst7: 78, lst7: -90};
              const obj = Object.entries(data).reduce((acc, [key, value]) =>{
              acc["lowest"] = acc["lowest"] ? Object.values(acc["lowest"])[0] < value ? acc["lowest"] : {[key] : value} : {[key] : value };
              return acc;
              }, {});
              console.log(`Lowest value is found in ${JSON.stringify(obj.lowest)}`);








              share|improve this answer


























                Your Answer






                StackExchange.ifUsing("editor", function () {
                StackExchange.using("externalEditor", function () {
                StackExchange.using("snippets", function () {
                StackExchange.snippets.init();
                });
                });
                }, "code-snippets");

                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "1"
                };
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function() {
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled) {
                StackExchange.using("snippets", function() {
                createEditor();
                });
                }
                else {
                createEditor();
                }
                });

                function createEditor() {
                StackExchange.prepareEditor({
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                });


                }
                });






                Mernt is a new contributor. Be nice, and check out our Code of Conduct.










                draft saved

                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55332453%2fgetting-the-lowest-value-with-key-in-array%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                9 Answers
                9






                active

                oldest

                votes








                9 Answers
                9






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                11














                You can get the key and value using Object.entries:






                var arr = {
                lst1: 300,
                lst2: 381,
                lst3: 4,
                lst4: 4,
                lst5: 49
                };

                function lowestValueAndKey(obj) {
                var lowestItems = Object.entries(obj).sort(([ ,v1], [ ,v2]) => v1 - v2)[0];
                return `Lowest value is ${lowestItems[1]}, with a key of ${lowestItems[0]}`;
                }

                var lowest = lowestValueAndKey(arr);
                console.log(lowest);








                share|improve this answer
























                • Hello, Are there an way to make this script compatible with Internet Explorer too?

                  – Mernt
                  Mar 25 at 7:41











                • @Mernt developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…

                  – JollyJoker
                  Mar 25 at 7:59











                • I tried but I'm getting a syntax error on line var lowestItems = Object.entries(arr).sort(([ ,v1], [ ,v2]) => v1 - v2)[0]

                  – Mernt
                  Mar 25 at 8:06











                • If you look here developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… you'll see that Internet Explorer does not support Object.entries

                  – ChatterOne
                  Mar 25 at 8:08






                • 1





                  @Mernt IE doesn't support the fat arrow notation. You need to rewrite ([ ,v1], [ ,v2]) => v1 - v2 in old style Javascript

                  – JollyJoker
                  Mar 25 at 8:12
















                11














                You can get the key and value using Object.entries:






                var arr = {
                lst1: 300,
                lst2: 381,
                lst3: 4,
                lst4: 4,
                lst5: 49
                };

                function lowestValueAndKey(obj) {
                var lowestItems = Object.entries(obj).sort(([ ,v1], [ ,v2]) => v1 - v2)[0];
                return `Lowest value is ${lowestItems[1]}, with a key of ${lowestItems[0]}`;
                }

                var lowest = lowestValueAndKey(arr);
                console.log(lowest);








                share|improve this answer
























                • Hello, Are there an way to make this script compatible with Internet Explorer too?

                  – Mernt
                  Mar 25 at 7:41











                • @Mernt developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…

                  – JollyJoker
                  Mar 25 at 7:59











                • I tried but I'm getting a syntax error on line var lowestItems = Object.entries(arr).sort(([ ,v1], [ ,v2]) => v1 - v2)[0]

                  – Mernt
                  Mar 25 at 8:06











                • If you look here developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… you'll see that Internet Explorer does not support Object.entries

                  – ChatterOne
                  Mar 25 at 8:08






                • 1





                  @Mernt IE doesn't support the fat arrow notation. You need to rewrite ([ ,v1], [ ,v2]) => v1 - v2 in old style Javascript

                  – JollyJoker
                  Mar 25 at 8:12














                11












                11








                11







                You can get the key and value using Object.entries:






                var arr = {
                lst1: 300,
                lst2: 381,
                lst3: 4,
                lst4: 4,
                lst5: 49
                };

                function lowestValueAndKey(obj) {
                var lowestItems = Object.entries(obj).sort(([ ,v1], [ ,v2]) => v1 - v2)[0];
                return `Lowest value is ${lowestItems[1]}, with a key of ${lowestItems[0]}`;
                }

                var lowest = lowestValueAndKey(arr);
                console.log(lowest);








                share|improve this answer













                You can get the key and value using Object.entries:






                var arr = {
                lst1: 300,
                lst2: 381,
                lst3: 4,
                lst4: 4,
                lst5: 49
                };

                function lowestValueAndKey(obj) {
                var lowestItems = Object.entries(obj).sort(([ ,v1], [ ,v2]) => v1 - v2)[0];
                return `Lowest value is ${lowestItems[1]}, with a key of ${lowestItems[0]}`;
                }

                var lowest = lowestValueAndKey(arr);
                console.log(lowest);








                var arr = {
                lst1: 300,
                lst2: 381,
                lst3: 4,
                lst4: 4,
                lst5: 49
                };

                function lowestValueAndKey(obj) {
                var lowestItems = Object.entries(obj).sort(([ ,v1], [ ,v2]) => v1 - v2)[0];
                return `Lowest value is ${lowestItems[1]}, with a key of ${lowestItems[0]}`;
                }

                var lowest = lowestValueAndKey(arr);
                console.log(lowest);





                var arr = {
                lst1: 300,
                lst2: 381,
                lst3: 4,
                lst4: 4,
                lst5: 49
                };

                function lowestValueAndKey(obj) {
                var lowestItems = Object.entries(obj).sort(([ ,v1], [ ,v2]) => v1 - v2)[0];
                return `Lowest value is ${lowestItems[1]}, with a key of ${lowestItems[0]}`;
                }

                var lowest = lowestValueAndKey(arr);
                console.log(lowest);






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 25 at 6:54









                Jack BashfordJack Bashford

                13.7k31848




                13.7k31848













                • Hello, Are there an way to make this script compatible with Internet Explorer too?

                  – Mernt
                  Mar 25 at 7:41











                • @Mernt developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…

                  – JollyJoker
                  Mar 25 at 7:59











                • I tried but I'm getting a syntax error on line var lowestItems = Object.entries(arr).sort(([ ,v1], [ ,v2]) => v1 - v2)[0]

                  – Mernt
                  Mar 25 at 8:06











                • If you look here developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… you'll see that Internet Explorer does not support Object.entries

                  – ChatterOne
                  Mar 25 at 8:08






                • 1





                  @Mernt IE doesn't support the fat arrow notation. You need to rewrite ([ ,v1], [ ,v2]) => v1 - v2 in old style Javascript

                  – JollyJoker
                  Mar 25 at 8:12



















                • Hello, Are there an way to make this script compatible with Internet Explorer too?

                  – Mernt
                  Mar 25 at 7:41











                • @Mernt developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…

                  – JollyJoker
                  Mar 25 at 7:59











                • I tried but I'm getting a syntax error on line var lowestItems = Object.entries(arr).sort(([ ,v1], [ ,v2]) => v1 - v2)[0]

                  – Mernt
                  Mar 25 at 8:06











                • If you look here developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… you'll see that Internet Explorer does not support Object.entries

                  – ChatterOne
                  Mar 25 at 8:08






                • 1





                  @Mernt IE doesn't support the fat arrow notation. You need to rewrite ([ ,v1], [ ,v2]) => v1 - v2 in old style Javascript

                  – JollyJoker
                  Mar 25 at 8:12

















                Hello, Are there an way to make this script compatible with Internet Explorer too?

                – Mernt
                Mar 25 at 7:41





                Hello, Are there an way to make this script compatible with Internet Explorer too?

                – Mernt
                Mar 25 at 7:41













                @Mernt developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…

                – JollyJoker
                Mar 25 at 7:59





                @Mernt developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…

                – JollyJoker
                Mar 25 at 7:59













                I tried but I'm getting a syntax error on line var lowestItems = Object.entries(arr).sort(([ ,v1], [ ,v2]) => v1 - v2)[0]

                – Mernt
                Mar 25 at 8:06





                I tried but I'm getting a syntax error on line var lowestItems = Object.entries(arr).sort(([ ,v1], [ ,v2]) => v1 - v2)[0]

                – Mernt
                Mar 25 at 8:06













                If you look here developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… you'll see that Internet Explorer does not support Object.entries

                – ChatterOne
                Mar 25 at 8:08





                If you look here developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… you'll see that Internet Explorer does not support Object.entries

                – ChatterOne
                Mar 25 at 8:08




                1




                1





                @Mernt IE doesn't support the fat arrow notation. You need to rewrite ([ ,v1], [ ,v2]) => v1 - v2 in old style Javascript

                – JollyJoker
                Mar 25 at 8:12





                @Mernt IE doesn't support the fat arrow notation. You need to rewrite ([ ,v1], [ ,v2]) => v1 - v2 in old style Javascript

                – JollyJoker
                Mar 25 at 8:12













                8














                There are several ways to arrive at what you want. Please see the following methods:






                const obj = { lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49 }
                const values = Object.values(obj)
                const lowest = Math.min.apply(null, values)

                // Using Object.keys(), Object.values() and findIndex()
                const keys = Object.keys(obj)
                const indexOfLowest = values.findIndex(function (x) { return x === lowest })
                console.log(`The lowest value is ${lowest} from ${keys[indexOfLowest]}`)



                // OR Using Object.keys() and filter()
                const key = Object.keys(obj).filter(function (x) { return obj[x] === lowest })[0]
                console.log(`The lowest value is ${lowest} from ${key}`)



                // OR Using Object.entries() and sort()
                const [[lowestKey, lowestVal]] = Object.entries(obj).sort(function ([,valA], [,valB]) { return valA - valB });
                console.log(`The lowest value is ${lowestVal} from ${lowestKey}`)








                share|improve this answer


























                • It should work as is in IE > = 11

                  – John Kennedy
                  Mar 25 at 8:02











                • Syntax error on line var key = Object.keys(arr).filter(x => arr[x] === lowest)[0]

                  – Mernt
                  Mar 25 at 8:08






                • 1





                  I'll suggest using a polyfill if it doesn't work in IE. See here: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… I should also inform you that on my IE on Windows 10 it works without needing a polyfill.

                  – John Kennedy
                  Mar 25 at 8:10








                • 1





                  @JohnKennedy I'd be surprised by that, IE doesn't support arrow functions

                  – Nick A
                  Mar 25 at 14:47
















                8














                There are several ways to arrive at what you want. Please see the following methods:






                const obj = { lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49 }
                const values = Object.values(obj)
                const lowest = Math.min.apply(null, values)

                // Using Object.keys(), Object.values() and findIndex()
                const keys = Object.keys(obj)
                const indexOfLowest = values.findIndex(function (x) { return x === lowest })
                console.log(`The lowest value is ${lowest} from ${keys[indexOfLowest]}`)



                // OR Using Object.keys() and filter()
                const key = Object.keys(obj).filter(function (x) { return obj[x] === lowest })[0]
                console.log(`The lowest value is ${lowest} from ${key}`)



                // OR Using Object.entries() and sort()
                const [[lowestKey, lowestVal]] = Object.entries(obj).sort(function ([,valA], [,valB]) { return valA - valB });
                console.log(`The lowest value is ${lowestVal} from ${lowestKey}`)








                share|improve this answer


























                • It should work as is in IE > = 11

                  – John Kennedy
                  Mar 25 at 8:02











                • Syntax error on line var key = Object.keys(arr).filter(x => arr[x] === lowest)[0]

                  – Mernt
                  Mar 25 at 8:08






                • 1





                  I'll suggest using a polyfill if it doesn't work in IE. See here: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… I should also inform you that on my IE on Windows 10 it works without needing a polyfill.

                  – John Kennedy
                  Mar 25 at 8:10








                • 1





                  @JohnKennedy I'd be surprised by that, IE doesn't support arrow functions

                  – Nick A
                  Mar 25 at 14:47














                8












                8








                8







                There are several ways to arrive at what you want. Please see the following methods:






                const obj = { lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49 }
                const values = Object.values(obj)
                const lowest = Math.min.apply(null, values)

                // Using Object.keys(), Object.values() and findIndex()
                const keys = Object.keys(obj)
                const indexOfLowest = values.findIndex(function (x) { return x === lowest })
                console.log(`The lowest value is ${lowest} from ${keys[indexOfLowest]}`)



                // OR Using Object.keys() and filter()
                const key = Object.keys(obj).filter(function (x) { return obj[x] === lowest })[0]
                console.log(`The lowest value is ${lowest} from ${key}`)



                // OR Using Object.entries() and sort()
                const [[lowestKey, lowestVal]] = Object.entries(obj).sort(function ([,valA], [,valB]) { return valA - valB });
                console.log(`The lowest value is ${lowestVal} from ${lowestKey}`)








                share|improve this answer















                There are several ways to arrive at what you want. Please see the following methods:






                const obj = { lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49 }
                const values = Object.values(obj)
                const lowest = Math.min.apply(null, values)

                // Using Object.keys(), Object.values() and findIndex()
                const keys = Object.keys(obj)
                const indexOfLowest = values.findIndex(function (x) { return x === lowest })
                console.log(`The lowest value is ${lowest} from ${keys[indexOfLowest]}`)



                // OR Using Object.keys() and filter()
                const key = Object.keys(obj).filter(function (x) { return obj[x] === lowest })[0]
                console.log(`The lowest value is ${lowest} from ${key}`)



                // OR Using Object.entries() and sort()
                const [[lowestKey, lowestVal]] = Object.entries(obj).sort(function ([,valA], [,valB]) { return valA - valB });
                console.log(`The lowest value is ${lowestVal} from ${lowestKey}`)








                const obj = { lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49 }
                const values = Object.values(obj)
                const lowest = Math.min.apply(null, values)

                // Using Object.keys(), Object.values() and findIndex()
                const keys = Object.keys(obj)
                const indexOfLowest = values.findIndex(function (x) { return x === lowest })
                console.log(`The lowest value is ${lowest} from ${keys[indexOfLowest]}`)



                // OR Using Object.keys() and filter()
                const key = Object.keys(obj).filter(function (x) { return obj[x] === lowest })[0]
                console.log(`The lowest value is ${lowest} from ${key}`)



                // OR Using Object.entries() and sort()
                const [[lowestKey, lowestVal]] = Object.entries(obj).sort(function ([,valA], [,valB]) { return valA - valB });
                console.log(`The lowest value is ${lowestVal} from ${lowestKey}`)





                const obj = { lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49 }
                const values = Object.values(obj)
                const lowest = Math.min.apply(null, values)

                // Using Object.keys(), Object.values() and findIndex()
                const keys = Object.keys(obj)
                const indexOfLowest = values.findIndex(function (x) { return x === lowest })
                console.log(`The lowest value is ${lowest} from ${keys[indexOfLowest]}`)



                // OR Using Object.keys() and filter()
                const key = Object.keys(obj).filter(function (x) { return obj[x] === lowest })[0]
                console.log(`The lowest value is ${lowest} from ${key}`)



                // OR Using Object.entries() and sort()
                const [[lowestKey, lowestVal]] = Object.entries(obj).sort(function ([,valA], [,valB]) { return valA - valB });
                console.log(`The lowest value is ${lowestVal} from ${lowestKey}`)






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Mar 25 at 21:42

























                answered Mar 25 at 6:53









                John KennedyJohn Kennedy

                2,91521227




                2,91521227













                • It should work as is in IE > = 11

                  – John Kennedy
                  Mar 25 at 8:02











                • Syntax error on line var key = Object.keys(arr).filter(x => arr[x] === lowest)[0]

                  – Mernt
                  Mar 25 at 8:08






                • 1





                  I'll suggest using a polyfill if it doesn't work in IE. See here: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… I should also inform you that on my IE on Windows 10 it works without needing a polyfill.

                  – John Kennedy
                  Mar 25 at 8:10








                • 1





                  @JohnKennedy I'd be surprised by that, IE doesn't support arrow functions

                  – Nick A
                  Mar 25 at 14:47



















                • It should work as is in IE > = 11

                  – John Kennedy
                  Mar 25 at 8:02











                • Syntax error on line var key = Object.keys(arr).filter(x => arr[x] === lowest)[0]

                  – Mernt
                  Mar 25 at 8:08






                • 1





                  I'll suggest using a polyfill if it doesn't work in IE. See here: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… I should also inform you that on my IE on Windows 10 it works without needing a polyfill.

                  – John Kennedy
                  Mar 25 at 8:10








                • 1





                  @JohnKennedy I'd be surprised by that, IE doesn't support arrow functions

                  – Nick A
                  Mar 25 at 14:47

















                It should work as is in IE > = 11

                – John Kennedy
                Mar 25 at 8:02





                It should work as is in IE > = 11

                – John Kennedy
                Mar 25 at 8:02













                Syntax error on line var key = Object.keys(arr).filter(x => arr[x] === lowest)[0]

                – Mernt
                Mar 25 at 8:08





                Syntax error on line var key = Object.keys(arr).filter(x => arr[x] === lowest)[0]

                – Mernt
                Mar 25 at 8:08




                1




                1





                I'll suggest using a polyfill if it doesn't work in IE. See here: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… I should also inform you that on my IE on Windows 10 it works without needing a polyfill.

                – John Kennedy
                Mar 25 at 8:10







                I'll suggest using a polyfill if it doesn't work in IE. See here: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… I should also inform you that on my IE on Windows 10 it works without needing a polyfill.

                – John Kennedy
                Mar 25 at 8:10






                1




                1





                @JohnKennedy I'd be surprised by that, IE doesn't support arrow functions

                – Nick A
                Mar 25 at 14:47





                @JohnKennedy I'd be surprised by that, IE doesn't support arrow functions

                – Nick A
                Mar 25 at 14:47











                7














                There are many ways to approach your problem, but here is one simple alternative way to do it. Basically, it is a brute force way of doing things, whereby you iterate through every property to check for the value.



                const obj = {lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49};

                const getSmallest = function (object) {
                let smallestValue = Number.MAX_VALUE;
                let selectedKey = '';
                for (let key in object) {
                if (object[key] < smallestValue) {
                smallestValue = object[key];
                selectedKey = key;
                }
                };
                console.log(selectedKey, smallestValue)
                return `The lowest value is ${smallestValue} from ${selectedKey}`;
                };

                getSmallest(obj);





                share|improve this answer






























                  7














                  There are many ways to approach your problem, but here is one simple alternative way to do it. Basically, it is a brute force way of doing things, whereby you iterate through every property to check for the value.



                  const obj = {lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49};

                  const getSmallest = function (object) {
                  let smallestValue = Number.MAX_VALUE;
                  let selectedKey = '';
                  for (let key in object) {
                  if (object[key] < smallestValue) {
                  smallestValue = object[key];
                  selectedKey = key;
                  }
                  };
                  console.log(selectedKey, smallestValue)
                  return `The lowest value is ${smallestValue} from ${selectedKey}`;
                  };

                  getSmallest(obj);





                  share|improve this answer




























                    7












                    7








                    7







                    There are many ways to approach your problem, but here is one simple alternative way to do it. Basically, it is a brute force way of doing things, whereby you iterate through every property to check for the value.



                    const obj = {lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49};

                    const getSmallest = function (object) {
                    let smallestValue = Number.MAX_VALUE;
                    let selectedKey = '';
                    for (let key in object) {
                    if (object[key] < smallestValue) {
                    smallestValue = object[key];
                    selectedKey = key;
                    }
                    };
                    console.log(selectedKey, smallestValue)
                    return `The lowest value is ${smallestValue} from ${selectedKey}`;
                    };

                    getSmallest(obj);





                    share|improve this answer















                    There are many ways to approach your problem, but here is one simple alternative way to do it. Basically, it is a brute force way of doing things, whereby you iterate through every property to check for the value.



                    const obj = {lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49};

                    const getSmallest = function (object) {
                    let smallestValue = Number.MAX_VALUE;
                    let selectedKey = '';
                    for (let key in object) {
                    if (object[key] < smallestValue) {
                    smallestValue = object[key];
                    selectedKey = key;
                    }
                    };
                    console.log(selectedKey, smallestValue)
                    return `The lowest value is ${smallestValue} from ${selectedKey}`;
                    };

                    getSmallest(obj);






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Mar 25 at 8:12

























                    answered Mar 25 at 6:55







                    user10997785






























                        3














                        To get only the minimum number you can use Math.min(...Object.entries(arr).map(o => o[1])):






                        const arr = {lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49};
                        const lowestValue = Math.min(...Object.entries(arr).map(o => o[1]));

                        console.log(lowestValue);





                        And to get the object with the minimum value you can use Object.entries(arr).reduce((a, [k, v]) => a[Object.keys(a)[0]] < v ? a : {[k]: v}, {}):






                        const arr = {lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49};
                        const lowestObj = Object.entries(arr).reduce((a, [k, v]) => a[Object.keys(a)[0]] < v ? a : {[k]: v}, {});

                        console.log(lowestObj);








                        share|improve this answer






























                          3














                          To get only the minimum number you can use Math.min(...Object.entries(arr).map(o => o[1])):






                          const arr = {lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49};
                          const lowestValue = Math.min(...Object.entries(arr).map(o => o[1]));

                          console.log(lowestValue);





                          And to get the object with the minimum value you can use Object.entries(arr).reduce((a, [k, v]) => a[Object.keys(a)[0]] < v ? a : {[k]: v}, {}):






                          const arr = {lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49};
                          const lowestObj = Object.entries(arr).reduce((a, [k, v]) => a[Object.keys(a)[0]] < v ? a : {[k]: v}, {});

                          console.log(lowestObj);








                          share|improve this answer




























                            3












                            3








                            3







                            To get only the minimum number you can use Math.min(...Object.entries(arr).map(o => o[1])):






                            const arr = {lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49};
                            const lowestValue = Math.min(...Object.entries(arr).map(o => o[1]));

                            console.log(lowestValue);





                            And to get the object with the minimum value you can use Object.entries(arr).reduce((a, [k, v]) => a[Object.keys(a)[0]] < v ? a : {[k]: v}, {}):






                            const arr = {lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49};
                            const lowestObj = Object.entries(arr).reduce((a, [k, v]) => a[Object.keys(a)[0]] < v ? a : {[k]: v}, {});

                            console.log(lowestObj);








                            share|improve this answer















                            To get only the minimum number you can use Math.min(...Object.entries(arr).map(o => o[1])):






                            const arr = {lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49};
                            const lowestValue = Math.min(...Object.entries(arr).map(o => o[1]));

                            console.log(lowestValue);





                            And to get the object with the minimum value you can use Object.entries(arr).reduce((a, [k, v]) => a[Object.keys(a)[0]] < v ? a : {[k]: v}, {}):






                            const arr = {lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49};
                            const lowestObj = Object.entries(arr).reduce((a, [k, v]) => a[Object.keys(a)[0]] < v ? a : {[k]: v}, {});

                            console.log(lowestObj);








                            const arr = {lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49};
                            const lowestValue = Math.min(...Object.entries(arr).map(o => o[1]));

                            console.log(lowestValue);





                            const arr = {lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49};
                            const lowestValue = Math.min(...Object.entries(arr).map(o => o[1]));

                            console.log(lowestValue);





                            const arr = {lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49};
                            const lowestObj = Object.entries(arr).reduce((a, [k, v]) => a[Object.keys(a)[0]] < v ? a : {[k]: v}, {});

                            console.log(lowestObj);





                            const arr = {lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49};
                            const lowestObj = Object.entries(arr).reduce((a, [k, v]) => a[Object.keys(a)[0]] < v ? a : {[k]: v}, {});

                            console.log(lowestObj);






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Mar 25 at 8:01

























                            answered Mar 25 at 7:03









                            Yosvel QuinteroYosvel Quintero

                            12k42531




                            12k42531























                                2














                                You can try this:



                                obj = {lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49, lst9: 2}; // This is an object, That;s ehy you getting infinite

                                var arr = Object.keys( obj ).map(function ( key ) { return obj[key]; });

                                alert(Math.min.apply( null, arr ));


                                Fiddle: https://jsfiddle.net/ozvubmdr/






                                share|improve this answer




























                                  2














                                  You can try this:



                                  obj = {lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49, lst9: 2}; // This is an object, That;s ehy you getting infinite

                                  var arr = Object.keys( obj ).map(function ( key ) { return obj[key]; });

                                  alert(Math.min.apply( null, arr ));


                                  Fiddle: https://jsfiddle.net/ozvubmdr/






                                  share|improve this answer


























                                    2












                                    2








                                    2







                                    You can try this:



                                    obj = {lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49, lst9: 2}; // This is an object, That;s ehy you getting infinite

                                    var arr = Object.keys( obj ).map(function ( key ) { return obj[key]; });

                                    alert(Math.min.apply( null, arr ));


                                    Fiddle: https://jsfiddle.net/ozvubmdr/






                                    share|improve this answer













                                    You can try this:



                                    obj = {lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49, lst9: 2}; // This is an object, That;s ehy you getting infinite

                                    var arr = Object.keys( obj ).map(function ( key ) { return obj[key]; });

                                    alert(Math.min.apply( null, arr ));


                                    Fiddle: https://jsfiddle.net/ozvubmdr/







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Mar 25 at 6:51









                                    Ajay MakwanaAjay Makwana

                                    1,537926




                                    1,537926























                                        2














                                        Easy, though not optimal, as it traverses the array twice: find the smallest value, then find the key belonging to it.






                                        arr = {lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49, lst9: 2}
                                        min = Math.min(...Object.values(arr));
                                        minkey = Object.keys(arr).find(key => arr[key] = min);
                                        console.log(`Lowest is ${min} at ${minkey}`);








                                        share|improve this answer




























                                          2














                                          Easy, though not optimal, as it traverses the array twice: find the smallest value, then find the key belonging to it.






                                          arr = {lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49, lst9: 2}
                                          min = Math.min(...Object.values(arr));
                                          minkey = Object.keys(arr).find(key => arr[key] = min);
                                          console.log(`Lowest is ${min} at ${minkey}`);








                                          share|improve this answer


























                                            2












                                            2








                                            2







                                            Easy, though not optimal, as it traverses the array twice: find the smallest value, then find the key belonging to it.






                                            arr = {lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49, lst9: 2}
                                            min = Math.min(...Object.values(arr));
                                            minkey = Object.keys(arr).find(key => arr[key] = min);
                                            console.log(`Lowest is ${min} at ${minkey}`);








                                            share|improve this answer













                                            Easy, though not optimal, as it traverses the array twice: find the smallest value, then find the key belonging to it.






                                            arr = {lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49, lst9: 2}
                                            min = Math.min(...Object.values(arr));
                                            minkey = Object.keys(arr).find(key => arr[key] = min);
                                            console.log(`Lowest is ${min} at ${minkey}`);








                                            arr = {lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49, lst9: 2}
                                            min = Math.min(...Object.values(arr));
                                            minkey = Object.keys(arr).find(key => arr[key] = min);
                                            console.log(`Lowest is ${min} at ${minkey}`);





                                            arr = {lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49, lst9: 2}
                                            min = Math.min(...Object.values(arr));
                                            minkey = Object.keys(arr).find(key => arr[key] = min);
                                            console.log(`Lowest is ${min} at ${minkey}`);






                                            share|improve this answer












                                            share|improve this answer



                                            share|improve this answer










                                            answered Mar 25 at 6:54









                                            AmadanAmadan

                                            133k13148198




                                            133k13148198























                                                2














                                                You may try this:






                                                const arr = {lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49, lst9: 2}
                                                const minVal = Math.min(...Object.values(arr))
                                                const fromKey = Object.keys(arr).find(key => arr[key] === minVal)
                                                console.log(`The lowest value is ${minVal} from ${fromKey}`)








                                                share|improve this answer




























                                                  2














                                                  You may try this:






                                                  const arr = {lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49, lst9: 2}
                                                  const minVal = Math.min(...Object.values(arr))
                                                  const fromKey = Object.keys(arr).find(key => arr[key] === minVal)
                                                  console.log(`The lowest value is ${minVal} from ${fromKey}`)








                                                  share|improve this answer


























                                                    2












                                                    2








                                                    2







                                                    You may try this:






                                                    const arr = {lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49, lst9: 2}
                                                    const minVal = Math.min(...Object.values(arr))
                                                    const fromKey = Object.keys(arr).find(key => arr[key] === minVal)
                                                    console.log(`The lowest value is ${minVal} from ${fromKey}`)








                                                    share|improve this answer













                                                    You may try this:






                                                    const arr = {lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49, lst9: 2}
                                                    const minVal = Math.min(...Object.values(arr))
                                                    const fromKey = Object.keys(arr).find(key => arr[key] === minVal)
                                                    console.log(`The lowest value is ${minVal} from ${fromKey}`)








                                                    const arr = {lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49, lst9: 2}
                                                    const minVal = Math.min(...Object.values(arr))
                                                    const fromKey = Object.keys(arr).find(key => arr[key] === minVal)
                                                    console.log(`The lowest value is ${minVal} from ${fromKey}`)





                                                    const arr = {lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49, lst9: 2}
                                                    const minVal = Math.min(...Object.values(arr))
                                                    const fromKey = Object.keys(arr).find(key => arr[key] === minVal)
                                                    console.log(`The lowest value is ${minVal} from ${fromKey}`)






                                                    share|improve this answer












                                                    share|improve this answer



                                                    share|improve this answer










                                                    answered Mar 25 at 6:59









                                                    Pak Wah WongPak Wah Wong

                                                    38317




                                                    38317























                                                        2














                                                        Here we first assume that the min, key and value as first property and the value respectively. Then we iterate through all the elements and compare with minValue to find any value less than that in the object. If it is, then we update both minValue and minKey. Anyway, the arr type is an Object. it is not an array in javascript.



                                                        var arr = {lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49};
                                                        var minkey = Object.keys(arr)[0];
                                                        var minValue = arr[Object.keys(arr)[0]];
                                                        for (var key in arr) {
                                                        if (arr.hasOwnProperty(key)) {
                                                        if(arr[key] < minValue){
                                                        minValue = arr[key];
                                                        minKey = key;
                                                        }
                                                        }
                                                        }





                                                        share|improve this answer




























                                                          2














                                                          Here we first assume that the min, key and value as first property and the value respectively. Then we iterate through all the elements and compare with minValue to find any value less than that in the object. If it is, then we update both minValue and minKey. Anyway, the arr type is an Object. it is not an array in javascript.



                                                          var arr = {lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49};
                                                          var minkey = Object.keys(arr)[0];
                                                          var minValue = arr[Object.keys(arr)[0]];
                                                          for (var key in arr) {
                                                          if (arr.hasOwnProperty(key)) {
                                                          if(arr[key] < minValue){
                                                          minValue = arr[key];
                                                          minKey = key;
                                                          }
                                                          }
                                                          }





                                                          share|improve this answer


























                                                            2












                                                            2








                                                            2







                                                            Here we first assume that the min, key and value as first property and the value respectively. Then we iterate through all the elements and compare with minValue to find any value less than that in the object. If it is, then we update both minValue and minKey. Anyway, the arr type is an Object. it is not an array in javascript.



                                                            var arr = {lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49};
                                                            var minkey = Object.keys(arr)[0];
                                                            var minValue = arr[Object.keys(arr)[0]];
                                                            for (var key in arr) {
                                                            if (arr.hasOwnProperty(key)) {
                                                            if(arr[key] < minValue){
                                                            minValue = arr[key];
                                                            minKey = key;
                                                            }
                                                            }
                                                            }





                                                            share|improve this answer













                                                            Here we first assume that the min, key and value as first property and the value respectively. Then we iterate through all the elements and compare with minValue to find any value less than that in the object. If it is, then we update both minValue and minKey. Anyway, the arr type is an Object. it is not an array in javascript.



                                                            var arr = {lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49};
                                                            var minkey = Object.keys(arr)[0];
                                                            var minValue = arr[Object.keys(arr)[0]];
                                                            for (var key in arr) {
                                                            if (arr.hasOwnProperty(key)) {
                                                            if(arr[key] < minValue){
                                                            minValue = arr[key];
                                                            minKey = key;
                                                            }
                                                            }
                                                            }






                                                            share|improve this answer












                                                            share|improve this answer



                                                            share|improve this answer










                                                            answered Mar 25 at 7:02









                                                            HarshanaHarshana

                                                            338211




                                                            338211























                                                                2














                                                                You can use Object.entries and use Array.reduce to reduce the resulting [key-value] pair to an object which will have lowest entry:






                                                                const data = {lst1: 300, lst2: -381, lst3: 4, lst4: 4, lst5: 49, lst6: -49, lst7: 0, lst7: 78, lst7: -90};
                                                                const obj = Object.entries(data).reduce((acc, [key, value]) =>{
                                                                acc["lowest"] = acc["lowest"] ? Object.values(acc["lowest"])[0] < value ? acc["lowest"] : {[key] : value} : {[key] : value };
                                                                return acc;
                                                                }, {});
                                                                console.log(`Lowest value is found in ${JSON.stringify(obj.lowest)}`);








                                                                share|improve this answer






























                                                                  2














                                                                  You can use Object.entries and use Array.reduce to reduce the resulting [key-value] pair to an object which will have lowest entry:






                                                                  const data = {lst1: 300, lst2: -381, lst3: 4, lst4: 4, lst5: 49, lst6: -49, lst7: 0, lst7: 78, lst7: -90};
                                                                  const obj = Object.entries(data).reduce((acc, [key, value]) =>{
                                                                  acc["lowest"] = acc["lowest"] ? Object.values(acc["lowest"])[0] < value ? acc["lowest"] : {[key] : value} : {[key] : value };
                                                                  return acc;
                                                                  }, {});
                                                                  console.log(`Lowest value is found in ${JSON.stringify(obj.lowest)}`);








                                                                  share|improve this answer




























                                                                    2












                                                                    2








                                                                    2







                                                                    You can use Object.entries and use Array.reduce to reduce the resulting [key-value] pair to an object which will have lowest entry:






                                                                    const data = {lst1: 300, lst2: -381, lst3: 4, lst4: 4, lst5: 49, lst6: -49, lst7: 0, lst7: 78, lst7: -90};
                                                                    const obj = Object.entries(data).reduce((acc, [key, value]) =>{
                                                                    acc["lowest"] = acc["lowest"] ? Object.values(acc["lowest"])[0] < value ? acc["lowest"] : {[key] : value} : {[key] : value };
                                                                    return acc;
                                                                    }, {});
                                                                    console.log(`Lowest value is found in ${JSON.stringify(obj.lowest)}`);








                                                                    share|improve this answer















                                                                    You can use Object.entries and use Array.reduce to reduce the resulting [key-value] pair to an object which will have lowest entry:






                                                                    const data = {lst1: 300, lst2: -381, lst3: 4, lst4: 4, lst5: 49, lst6: -49, lst7: 0, lst7: 78, lst7: -90};
                                                                    const obj = Object.entries(data).reduce((acc, [key, value]) =>{
                                                                    acc["lowest"] = acc["lowest"] ? Object.values(acc["lowest"])[0] < value ? acc["lowest"] : {[key] : value} : {[key] : value };
                                                                    return acc;
                                                                    }, {});
                                                                    console.log(`Lowest value is found in ${JSON.stringify(obj.lowest)}`);








                                                                    const data = {lst1: 300, lst2: -381, lst3: 4, lst4: 4, lst5: 49, lst6: -49, lst7: 0, lst7: 78, lst7: -90};
                                                                    const obj = Object.entries(data).reduce((acc, [key, value]) =>{
                                                                    acc["lowest"] = acc["lowest"] ? Object.values(acc["lowest"])[0] < value ? acc["lowest"] : {[key] : value} : {[key] : value };
                                                                    return acc;
                                                                    }, {});
                                                                    console.log(`Lowest value is found in ${JSON.stringify(obj.lowest)}`);





                                                                    const data = {lst1: 300, lst2: -381, lst3: 4, lst4: 4, lst5: 49, lst6: -49, lst7: 0, lst7: 78, lst7: -90};
                                                                    const obj = Object.entries(data).reduce((acc, [key, value]) =>{
                                                                    acc["lowest"] = acc["lowest"] ? Object.values(acc["lowest"])[0] < value ? acc["lowest"] : {[key] : value} : {[key] : value };
                                                                    return acc;
                                                                    }, {});
                                                                    console.log(`Lowest value is found in ${JSON.stringify(obj.lowest)}`);






                                                                    share|improve this answer














                                                                    share|improve this answer



                                                                    share|improve this answer








                                                                    edited Mar 25 at 7:30

























                                                                    answered Mar 25 at 7:02









                                                                    Amardeep BhowmickAmardeep Bhowmick

                                                                    5,03321128




                                                                    5,03321128






















                                                                        Mernt is a new contributor. Be nice, and check out our Code of Conduct.










                                                                        draft saved

                                                                        draft discarded


















                                                                        Mernt is a new contributor. Be nice, and check out our Code of Conduct.













                                                                        Mernt is a new contributor. Be nice, and check out our Code of Conduct.












                                                                        Mernt is a new contributor. Be nice, and check out our Code of Conduct.
















                                                                        Thanks for contributing an answer to Stack Overflow!


                                                                        • Please be sure to answer the question. Provide details and share your research!

                                                                        But avoid



                                                                        • Asking for help, clarification, or responding to other answers.

                                                                        • Making statements based on opinion; back them up with references or personal experience.


                                                                        To learn more, see our tips on writing great answers.




                                                                        draft saved


                                                                        draft discarded














                                                                        StackExchange.ready(
                                                                        function () {
                                                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55332453%2fgetting-the-lowest-value-with-key-in-array%23new-answer', 'question_page');
                                                                        }
                                                                        );

                                                                        Post as a guest















                                                                        Required, but never shown





















































                                                                        Required, but never shown














                                                                        Required, but never shown












                                                                        Required, but never shown







                                                                        Required, but never shown

































                                                                        Required, but never shown














                                                                        Required, but never shown












                                                                        Required, but never shown







                                                                        Required, but never shown







                                                                        Popular posts from this blog

                                                                        Plaza Victoria

                                                                        In PowerPoint, is there a keyboard shortcut for bulleted / numbered list?

                                                                        How to put 3 figures in Latex with 2 figures side by side and 1 below these side by side images but in...