Getting the lowest value with key in arrayget key from a javascript object with minimum valueCreate ArrayList from arrayHow do I check if an array includes an object in JavaScript?How to append something to an array?Checking if a key exists in a JavaScript object?Sort array of objects by string property valueLoop through an array in JavaScriptHow to check if an object is an array?How do I remove a particular element from an array in JavaScript?Copy array by valueFor-each over an array in JavaScript?

What are the G forces leaving Earth orbit?

Why didn't Boeing produce its own regional jet?

Does Dispel Magic work on Tiny Hut?

What do you call someone who asks many questions?

Is it "common practice in Fourier transform spectroscopy to multiply the measured interferogram by an apodizing function"? If so, why?

Why was Sir Cadogan fired?

Is there an expression that means doing something right before you will need it rather than doing it in case you might need it?

Unlock My Phone! February 2018

How to travel to Japan while expressing milk?

In Bayesian inference, why are some terms dropped from the posterior predictive?

Was the Stack Exchange "Happy April Fools" page fitting with the '90's code?

How exploitable/balanced is this homebrew spell: Spell Permanency?

Does Fukaya see all symplectic topology?

Why were 5.25" floppy drives cheaper than 8"?

Chinese modern vocabulary 包饺子

What historical events would have to change in order to make 19th century "steampunk" technology possible?

Can I set a Ready action to trigger when literally anything happens?

How to prevent "they're falling in love" trope

How can I deal with my CEO asking me to hire someone with a higher salary than me, a co-founder?

Why was the shrink from 8″ made only to 5.25″ and not smaller (4″ or less)

Can someone clarify Hamming's notion of important problems in relation to modern academia?

Venezuelan girlfriend wants to travel the USA to be with me. What is the process?

What is a Samsaran Word™?

meaning of 腰を落としている



Getting the lowest value with key in array


get key from a javascript object with minimum valueCreate ArrayList from arrayHow do I check if an array includes an object in JavaScript?How to append something to an array?Checking if a key exists in a JavaScript object?Sort array of objects by string property valueLoop through an array in JavaScriptHow to check if an object is an array?How do I remove a particular element from an array in JavaScript?Copy array by valueFor-each over an array in JavaScript?













14















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
























  • What is expected output?

    – Maheer Ali
    Mar 25 at 6:45






  • 3





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

    – Jack Bashford
    Mar 25 at 6:47






  • 3





    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







  • 10





    arr is not an array but an object

    – Isaac
    Mar 25 at 6:53















14















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
























  • What is expected output?

    – Maheer Ali
    Mar 25 at 6:45






  • 3





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

    – Jack Bashford
    Mar 25 at 6:47






  • 3





    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







  • 10





    arr is not an array but an object

    – Isaac
    Mar 25 at 6:53













14












14








14


2






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
















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















share|improve this question













share|improve this question




share|improve this question








edited Mar 25 at 20:38









Jack Bashford

14.6k31848




14.6k31848










asked Mar 25 at 6:44









MerntMernt

795




795












  • What is expected output?

    – Maheer Ali
    Mar 25 at 6:45






  • 3





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

    – Jack Bashford
    Mar 25 at 6:47






  • 3





    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







  • 10





    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






  • 3





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

    – Jack Bashford
    Mar 25 at 6:47






  • 3





    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







  • 10





    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




3




3





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




3




3





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





10




10





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


















14














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


















  • 1





    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






  • 2





    @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


















9














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
































    4














    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














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














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



                );













                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









                14














                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


















                • 1





                  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






                • 2





                  @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















                14














                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


















                • 1





                  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






                • 2





                  @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













                14












                14








                14







                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

                14.6k31848




                14.6k31848







                • 1





                  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






                • 2





                  @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












                • 1





                  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






                • 2





                  @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







                1




                1





                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




                2




                2





                @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













                9














                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















                9














                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













                9












                9








                9







                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

                3,00021227




                3,00021227












                • 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











                8














                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





























                  8














                  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



























                    8












                    8








                    8







                    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




























                        4














                        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





























                          4














                          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



























                            4












                            4








                            4







                            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





















                                3














                                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



























                                  3














                                  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

























                                    3












                                    3








                                    3







                                    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

                                    348211




                                    348211





















                                        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

                                                    133k13148199




                                                    133k13148199





















                                                        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














                                                                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,33821128




                                                                    5,33821128



























                                                                        draft saved

                                                                        draft discarded
















































                                                                        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