How to get a key in a JavaScript Map by its value?












15















I have a js Map like this one



let people = new Map();
people.set('1', 'jhon');
people.set('2', 'jasmein');
people.set('3', 'abdo');


what I want is some method to return a key by its value



let jhonKey = people.getKey('jhon'); // jhonKey should be '1'









share|improve this question




















  • 13





    Then why did you store it the wrong way round? :-P

    – Bergi
    Nov 6 '17 at 16:06
















15















I have a js Map like this one



let people = new Map();
people.set('1', 'jhon');
people.set('2', 'jasmein');
people.set('3', 'abdo');


what I want is some method to return a key by its value



let jhonKey = people.getKey('jhon'); // jhonKey should be '1'









share|improve this question




















  • 13





    Then why did you store it the wrong way round? :-P

    – Bergi
    Nov 6 '17 at 16:06














15












15








15


4






I have a js Map like this one



let people = new Map();
people.set('1', 'jhon');
people.set('2', 'jasmein');
people.set('3', 'abdo');


what I want is some method to return a key by its value



let jhonKey = people.getKey('jhon'); // jhonKey should be '1'









share|improve this question
















I have a js Map like this one



let people = new Map();
people.set('1', 'jhon');
people.set('2', 'jasmein');
people.set('3', 'abdo');


what I want is some method to return a key by its value



let jhonKey = people.getKey('jhon'); // jhonKey should be '1'






javascript dictionary






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 6 '17 at 11:17









Cerbrus

49.7k1094116




49.7k1094116










asked Nov 6 '17 at 11:13









Engineer PassionEngineer Passion

146111




146111








  • 13





    Then why did you store it the wrong way round? :-P

    – Bergi
    Nov 6 '17 at 16:06














  • 13





    Then why did you store it the wrong way round? :-P

    – Bergi
    Nov 6 '17 at 16:06








13




13





Then why did you store it the wrong way round? :-P

– Bergi
Nov 6 '17 at 16:06





Then why did you store it the wrong way round? :-P

– Bergi
Nov 6 '17 at 16:06












4 Answers
4






active

oldest

votes


















17














You could seach for it in an array of entries.






let people = new Map();
people.set('1', 'jhon');
people.set('2', 'jasmein');
people.set('3', 'abdo');

let jhonKeys = [...people.entries()]
.filter(({ 1: v }) => v === 'jhon')
.map(([k]) => k);

console.log(jhonKeys); // if empty, no key foudn otherwise all found keys.








share|improve this answer


























  • @Rajesh, actually, we need the key, not the index.

    – Nina Scholz
    Nov 6 '17 at 11:26








  • 2





    Actually we need the key not the index or value.. :)

    – Keith
    Nov 6 '17 at 11:26











  • @NinaScholz I am curious, are you really using this kind of one-liners in your job, and whether you are debugging your code in any way?

    – Engineer
    Nov 9 '17 at 12:58






  • 1





    @NinaScholz You don't need the array access and || logic if you destructure the map values directly: [...people.values()]

    – philraj
    Sep 12 '18 at 20:40








  • 1





    @philraj, i changed the answer. anothher solution could be the use of Array.from with a mapping of the value.

    – Nina Scholz
    Sep 12 '18 at 20:49





















18














You can use for..of loop to loop directly over the map.entries and get the keys.






function getByValue(map, searchValue) {
for (let [key, value] of map.entries()) {
if (value === searchValue)
return key;
}
}

let people = new Map();
people.set('1', 'jhon');
people.set('2', 'jasmein');
people.set('3', 'abdo');

console.log(getByValue(people, 'jhon'))
console.log(getByValue(people, 'abdo'))








share|improve this answer





















  • 4





    One advantage to this solution is possibly speed, as there is no array transform. To really get into the spirit of ES6 though, might be nice if you change the var to a const..

    – Keith
    Nov 6 '17 at 13:15











  • @Keith Thanks for pointing it out. Have updated it to let.

    – Rajesh
    Nov 6 '17 at 13:22






  • 1





    Nice one,. just a heads up const works here too in for of. It's doesn't work like a normal for in this regard.

    – Keith
    Nov 6 '17 at 13:28






  • 1





    Just my personal preference to use let over const.

    – Rajesh
    Nov 6 '17 at 13:38











  • You don't need the map.entries(), the map itself acts as an iterator. I submitted an edit.

    – philraj
    Sep 12 '18 at 20:44



















7














There is no direct method for picking out information in this direction, so if all you have is the map you need to loop through the set as suggested by others.



If the map/array/other is large enough that such a loop would be a performance issue and the requirement for a reverse lookup is common within the project, you could implement your own structure using a pair of maps/arrays/other with one as per the current object and the other with the key and value reversed. That way the reverse lookup is as efficient as the normal one. Of course, you have more work to do as you need to implement each method that you need as a pass-through to one or both of the underlying objects so if the map is small and/or the reverse lookup is not needed often the scan-via-loop option is likely to be preferable due to being simpler to maintain and possible simpler for the JiT compiler to optimise.



In any case one thing to be wary of is the possibility that multiple keys could have the same value. If this is possible then when looping the through your map you need to decide if you are fine to return one of the possible keys arbitrarily (probably the first one) or if you want to return an array of keys, and if implementing a reverse index for data that could have duplicate values the same issue also needs to be accounted for.






share|improve this answer































    1














    Though late and other great answers already exist, still you can give below "..." and "Array.find" a try






    let people = new Map();
    people.set('1', 'jhon');
    people.set('2', 'jasmein');
    people.set('3', 'abdo');

    function getKey(value) {
    return [...people].find(([key, val]) => val == value)[0]
    }

    console.log('Jasmein - ',getKey('jasmein'))

    console.log('Jhon - ',getKey('jhon'))








    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%2f47135661%2fhow-to-get-a-key-in-a-javascript-map-by-its-value%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      17














      You could seach for it in an array of entries.






      let people = new Map();
      people.set('1', 'jhon');
      people.set('2', 'jasmein');
      people.set('3', 'abdo');

      let jhonKeys = [...people.entries()]
      .filter(({ 1: v }) => v === 'jhon')
      .map(([k]) => k);

      console.log(jhonKeys); // if empty, no key foudn otherwise all found keys.








      share|improve this answer


























      • @Rajesh, actually, we need the key, not the index.

        – Nina Scholz
        Nov 6 '17 at 11:26








      • 2





        Actually we need the key not the index or value.. :)

        – Keith
        Nov 6 '17 at 11:26











      • @NinaScholz I am curious, are you really using this kind of one-liners in your job, and whether you are debugging your code in any way?

        – Engineer
        Nov 9 '17 at 12:58






      • 1





        @NinaScholz You don't need the array access and || logic if you destructure the map values directly: [...people.values()]

        – philraj
        Sep 12 '18 at 20:40








      • 1





        @philraj, i changed the answer. anothher solution could be the use of Array.from with a mapping of the value.

        – Nina Scholz
        Sep 12 '18 at 20:49


















      17














      You could seach for it in an array of entries.






      let people = new Map();
      people.set('1', 'jhon');
      people.set('2', 'jasmein');
      people.set('3', 'abdo');

      let jhonKeys = [...people.entries()]
      .filter(({ 1: v }) => v === 'jhon')
      .map(([k]) => k);

      console.log(jhonKeys); // if empty, no key foudn otherwise all found keys.








      share|improve this answer


























      • @Rajesh, actually, we need the key, not the index.

        – Nina Scholz
        Nov 6 '17 at 11:26








      • 2





        Actually we need the key not the index or value.. :)

        – Keith
        Nov 6 '17 at 11:26











      • @NinaScholz I am curious, are you really using this kind of one-liners in your job, and whether you are debugging your code in any way?

        – Engineer
        Nov 9 '17 at 12:58






      • 1





        @NinaScholz You don't need the array access and || logic if you destructure the map values directly: [...people.values()]

        – philraj
        Sep 12 '18 at 20:40








      • 1





        @philraj, i changed the answer. anothher solution could be the use of Array.from with a mapping of the value.

        – Nina Scholz
        Sep 12 '18 at 20:49
















      17












      17








      17







      You could seach for it in an array of entries.






      let people = new Map();
      people.set('1', 'jhon');
      people.set('2', 'jasmein');
      people.set('3', 'abdo');

      let jhonKeys = [...people.entries()]
      .filter(({ 1: v }) => v === 'jhon')
      .map(([k]) => k);

      console.log(jhonKeys); // if empty, no key foudn otherwise all found keys.








      share|improve this answer















      You could seach for it in an array of entries.






      let people = new Map();
      people.set('1', 'jhon');
      people.set('2', 'jasmein');
      people.set('3', 'abdo');

      let jhonKeys = [...people.entries()]
      .filter(({ 1: v }) => v === 'jhon')
      .map(([k]) => k);

      console.log(jhonKeys); // if empty, no key foudn otherwise all found keys.








      let people = new Map();
      people.set('1', 'jhon');
      people.set('2', 'jasmein');
      people.set('3', 'abdo');

      let jhonKeys = [...people.entries()]
      .filter(({ 1: v }) => v === 'jhon')
      .map(([k]) => k);

      console.log(jhonKeys); // if empty, no key foudn otherwise all found keys.





      let people = new Map();
      people.set('1', 'jhon');
      people.set('2', 'jasmein');
      people.set('3', 'abdo');

      let jhonKeys = [...people.entries()]
      .filter(({ 1: v }) => v === 'jhon')
      .map(([k]) => k);

      console.log(jhonKeys); // if empty, no key foudn otherwise all found keys.






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Oct 9 '18 at 6:07

























      answered Nov 6 '17 at 11:17









      Nina ScholzNina Scholz

      189k1598172




      189k1598172













      • @Rajesh, actually, we need the key, not the index.

        – Nina Scholz
        Nov 6 '17 at 11:26








      • 2





        Actually we need the key not the index or value.. :)

        – Keith
        Nov 6 '17 at 11:26











      • @NinaScholz I am curious, are you really using this kind of one-liners in your job, and whether you are debugging your code in any way?

        – Engineer
        Nov 9 '17 at 12:58






      • 1





        @NinaScholz You don't need the array access and || logic if you destructure the map values directly: [...people.values()]

        – philraj
        Sep 12 '18 at 20:40








      • 1





        @philraj, i changed the answer. anothher solution could be the use of Array.from with a mapping of the value.

        – Nina Scholz
        Sep 12 '18 at 20:49





















      • @Rajesh, actually, we need the key, not the index.

        – Nina Scholz
        Nov 6 '17 at 11:26








      • 2





        Actually we need the key not the index or value.. :)

        – Keith
        Nov 6 '17 at 11:26











      • @NinaScholz I am curious, are you really using this kind of one-liners in your job, and whether you are debugging your code in any way?

        – Engineer
        Nov 9 '17 at 12:58






      • 1





        @NinaScholz You don't need the array access and || logic if you destructure the map values directly: [...people.values()]

        – philraj
        Sep 12 '18 at 20:40








      • 1





        @philraj, i changed the answer. anothher solution could be the use of Array.from with a mapping of the value.

        – Nina Scholz
        Sep 12 '18 at 20:49



















      @Rajesh, actually, we need the key, not the index.

      – Nina Scholz
      Nov 6 '17 at 11:26







      @Rajesh, actually, we need the key, not the index.

      – Nina Scholz
      Nov 6 '17 at 11:26






      2




      2





      Actually we need the key not the index or value.. :)

      – Keith
      Nov 6 '17 at 11:26





      Actually we need the key not the index or value.. :)

      – Keith
      Nov 6 '17 at 11:26













      @NinaScholz I am curious, are you really using this kind of one-liners in your job, and whether you are debugging your code in any way?

      – Engineer
      Nov 9 '17 at 12:58





      @NinaScholz I am curious, are you really using this kind of one-liners in your job, and whether you are debugging your code in any way?

      – Engineer
      Nov 9 '17 at 12:58




      1




      1





      @NinaScholz You don't need the array access and || logic if you destructure the map values directly: [...people.values()]

      – philraj
      Sep 12 '18 at 20:40







      @NinaScholz You don't need the array access and || logic if you destructure the map values directly: [...people.values()]

      – philraj
      Sep 12 '18 at 20:40






      1




      1





      @philraj, i changed the answer. anothher solution could be the use of Array.from with a mapping of the value.

      – Nina Scholz
      Sep 12 '18 at 20:49







      @philraj, i changed the answer. anothher solution could be the use of Array.from with a mapping of the value.

      – Nina Scholz
      Sep 12 '18 at 20:49















      18














      You can use for..of loop to loop directly over the map.entries and get the keys.






      function getByValue(map, searchValue) {
      for (let [key, value] of map.entries()) {
      if (value === searchValue)
      return key;
      }
      }

      let people = new Map();
      people.set('1', 'jhon');
      people.set('2', 'jasmein');
      people.set('3', 'abdo');

      console.log(getByValue(people, 'jhon'))
      console.log(getByValue(people, 'abdo'))








      share|improve this answer





















      • 4





        One advantage to this solution is possibly speed, as there is no array transform. To really get into the spirit of ES6 though, might be nice if you change the var to a const..

        – Keith
        Nov 6 '17 at 13:15











      • @Keith Thanks for pointing it out. Have updated it to let.

        – Rajesh
        Nov 6 '17 at 13:22






      • 1





        Nice one,. just a heads up const works here too in for of. It's doesn't work like a normal for in this regard.

        – Keith
        Nov 6 '17 at 13:28






      • 1





        Just my personal preference to use let over const.

        – Rajesh
        Nov 6 '17 at 13:38











      • You don't need the map.entries(), the map itself acts as an iterator. I submitted an edit.

        – philraj
        Sep 12 '18 at 20:44
















      18














      You can use for..of loop to loop directly over the map.entries and get the keys.






      function getByValue(map, searchValue) {
      for (let [key, value] of map.entries()) {
      if (value === searchValue)
      return key;
      }
      }

      let people = new Map();
      people.set('1', 'jhon');
      people.set('2', 'jasmein');
      people.set('3', 'abdo');

      console.log(getByValue(people, 'jhon'))
      console.log(getByValue(people, 'abdo'))








      share|improve this answer





















      • 4





        One advantage to this solution is possibly speed, as there is no array transform. To really get into the spirit of ES6 though, might be nice if you change the var to a const..

        – Keith
        Nov 6 '17 at 13:15











      • @Keith Thanks for pointing it out. Have updated it to let.

        – Rajesh
        Nov 6 '17 at 13:22






      • 1





        Nice one,. just a heads up const works here too in for of. It's doesn't work like a normal for in this regard.

        – Keith
        Nov 6 '17 at 13:28






      • 1





        Just my personal preference to use let over const.

        – Rajesh
        Nov 6 '17 at 13:38











      • You don't need the map.entries(), the map itself acts as an iterator. I submitted an edit.

        – philraj
        Sep 12 '18 at 20:44














      18












      18








      18







      You can use for..of loop to loop directly over the map.entries and get the keys.






      function getByValue(map, searchValue) {
      for (let [key, value] of map.entries()) {
      if (value === searchValue)
      return key;
      }
      }

      let people = new Map();
      people.set('1', 'jhon');
      people.set('2', 'jasmein');
      people.set('3', 'abdo');

      console.log(getByValue(people, 'jhon'))
      console.log(getByValue(people, 'abdo'))








      share|improve this answer















      You can use for..of loop to loop directly over the map.entries and get the keys.






      function getByValue(map, searchValue) {
      for (let [key, value] of map.entries()) {
      if (value === searchValue)
      return key;
      }
      }

      let people = new Map();
      people.set('1', 'jhon');
      people.set('2', 'jasmein');
      people.set('3', 'abdo');

      console.log(getByValue(people, 'jhon'))
      console.log(getByValue(people, 'abdo'))








      function getByValue(map, searchValue) {
      for (let [key, value] of map.entries()) {
      if (value === searchValue)
      return key;
      }
      }

      let people = new Map();
      people.set('1', 'jhon');
      people.set('2', 'jasmein');
      people.set('3', 'abdo');

      console.log(getByValue(people, 'jhon'))
      console.log(getByValue(people, 'abdo'))





      function getByValue(map, searchValue) {
      for (let [key, value] of map.entries()) {
      if (value === searchValue)
      return key;
      }
      }

      let people = new Map();
      people.set('1', 'jhon');
      people.set('2', 'jasmein');
      people.set('3', 'abdo');

      console.log(getByValue(people, 'jhon'))
      console.log(getByValue(people, 'abdo'))






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 6 '17 at 13:21

























      answered Nov 6 '17 at 11:33









      RajeshRajesh

      16.4k52253




      16.4k52253








      • 4





        One advantage to this solution is possibly speed, as there is no array transform. To really get into the spirit of ES6 though, might be nice if you change the var to a const..

        – Keith
        Nov 6 '17 at 13:15











      • @Keith Thanks for pointing it out. Have updated it to let.

        – Rajesh
        Nov 6 '17 at 13:22






      • 1





        Nice one,. just a heads up const works here too in for of. It's doesn't work like a normal for in this regard.

        – Keith
        Nov 6 '17 at 13:28






      • 1





        Just my personal preference to use let over const.

        – Rajesh
        Nov 6 '17 at 13:38











      • You don't need the map.entries(), the map itself acts as an iterator. I submitted an edit.

        – philraj
        Sep 12 '18 at 20:44














      • 4





        One advantage to this solution is possibly speed, as there is no array transform. To really get into the spirit of ES6 though, might be nice if you change the var to a const..

        – Keith
        Nov 6 '17 at 13:15











      • @Keith Thanks for pointing it out. Have updated it to let.

        – Rajesh
        Nov 6 '17 at 13:22






      • 1





        Nice one,. just a heads up const works here too in for of. It's doesn't work like a normal for in this regard.

        – Keith
        Nov 6 '17 at 13:28






      • 1





        Just my personal preference to use let over const.

        – Rajesh
        Nov 6 '17 at 13:38











      • You don't need the map.entries(), the map itself acts as an iterator. I submitted an edit.

        – philraj
        Sep 12 '18 at 20:44








      4




      4





      One advantage to this solution is possibly speed, as there is no array transform. To really get into the spirit of ES6 though, might be nice if you change the var to a const..

      – Keith
      Nov 6 '17 at 13:15





      One advantage to this solution is possibly speed, as there is no array transform. To really get into the spirit of ES6 though, might be nice if you change the var to a const..

      – Keith
      Nov 6 '17 at 13:15













      @Keith Thanks for pointing it out. Have updated it to let.

      – Rajesh
      Nov 6 '17 at 13:22





      @Keith Thanks for pointing it out. Have updated it to let.

      – Rajesh
      Nov 6 '17 at 13:22




      1




      1





      Nice one,. just a heads up const works here too in for of. It's doesn't work like a normal for in this regard.

      – Keith
      Nov 6 '17 at 13:28





      Nice one,. just a heads up const works here too in for of. It's doesn't work like a normal for in this regard.

      – Keith
      Nov 6 '17 at 13:28




      1




      1





      Just my personal preference to use let over const.

      – Rajesh
      Nov 6 '17 at 13:38





      Just my personal preference to use let over const.

      – Rajesh
      Nov 6 '17 at 13:38













      You don't need the map.entries(), the map itself acts as an iterator. I submitted an edit.

      – philraj
      Sep 12 '18 at 20:44





      You don't need the map.entries(), the map itself acts as an iterator. I submitted an edit.

      – philraj
      Sep 12 '18 at 20:44











      7














      There is no direct method for picking out information in this direction, so if all you have is the map you need to loop through the set as suggested by others.



      If the map/array/other is large enough that such a loop would be a performance issue and the requirement for a reverse lookup is common within the project, you could implement your own structure using a pair of maps/arrays/other with one as per the current object and the other with the key and value reversed. That way the reverse lookup is as efficient as the normal one. Of course, you have more work to do as you need to implement each method that you need as a pass-through to one or both of the underlying objects so if the map is small and/or the reverse lookup is not needed often the scan-via-loop option is likely to be preferable due to being simpler to maintain and possible simpler for the JiT compiler to optimise.



      In any case one thing to be wary of is the possibility that multiple keys could have the same value. If this is possible then when looping the through your map you need to decide if you are fine to return one of the possible keys arbitrarily (probably the first one) or if you want to return an array of keys, and if implementing a reverse index for data that could have duplicate values the same issue also needs to be accounted for.






      share|improve this answer




























        7














        There is no direct method for picking out information in this direction, so if all you have is the map you need to loop through the set as suggested by others.



        If the map/array/other is large enough that such a loop would be a performance issue and the requirement for a reverse lookup is common within the project, you could implement your own structure using a pair of maps/arrays/other with one as per the current object and the other with the key and value reversed. That way the reverse lookup is as efficient as the normal one. Of course, you have more work to do as you need to implement each method that you need as a pass-through to one or both of the underlying objects so if the map is small and/or the reverse lookup is not needed often the scan-via-loop option is likely to be preferable due to being simpler to maintain and possible simpler for the JiT compiler to optimise.



        In any case one thing to be wary of is the possibility that multiple keys could have the same value. If this is possible then when looping the through your map you need to decide if you are fine to return one of the possible keys arbitrarily (probably the first one) or if you want to return an array of keys, and if implementing a reverse index for data that could have duplicate values the same issue also needs to be accounted for.






        share|improve this answer


























          7












          7








          7







          There is no direct method for picking out information in this direction, so if all you have is the map you need to loop through the set as suggested by others.



          If the map/array/other is large enough that such a loop would be a performance issue and the requirement for a reverse lookup is common within the project, you could implement your own structure using a pair of maps/arrays/other with one as per the current object and the other with the key and value reversed. That way the reverse lookup is as efficient as the normal one. Of course, you have more work to do as you need to implement each method that you need as a pass-through to one or both of the underlying objects so if the map is small and/or the reverse lookup is not needed often the scan-via-loop option is likely to be preferable due to being simpler to maintain and possible simpler for the JiT compiler to optimise.



          In any case one thing to be wary of is the possibility that multiple keys could have the same value. If this is possible then when looping the through your map you need to decide if you are fine to return one of the possible keys arbitrarily (probably the first one) or if you want to return an array of keys, and if implementing a reverse index for data that could have duplicate values the same issue also needs to be accounted for.






          share|improve this answer













          There is no direct method for picking out information in this direction, so if all you have is the map you need to loop through the set as suggested by others.



          If the map/array/other is large enough that such a loop would be a performance issue and the requirement for a reverse lookup is common within the project, you could implement your own structure using a pair of maps/arrays/other with one as per the current object and the other with the key and value reversed. That way the reverse lookup is as efficient as the normal one. Of course, you have more work to do as you need to implement each method that you need as a pass-through to one or both of the underlying objects so if the map is small and/or the reverse lookup is not needed often the scan-via-loop option is likely to be preferable due to being simpler to maintain and possible simpler for the JiT compiler to optimise.



          In any case one thing to be wary of is the possibility that multiple keys could have the same value. If this is possible then when looping the through your map you need to decide if you are fine to return one of the possible keys arbitrarily (probably the first one) or if you want to return an array of keys, and if implementing a reverse index for data that could have duplicate values the same issue also needs to be accounted for.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 6 '17 at 15:36









          David SpillettDavid Spillett

          1,050917




          1,050917























              1














              Though late and other great answers already exist, still you can give below "..." and "Array.find" a try






              let people = new Map();
              people.set('1', 'jhon');
              people.set('2', 'jasmein');
              people.set('3', 'abdo');

              function getKey(value) {
              return [...people].find(([key, val]) => val == value)[0]
              }

              console.log('Jasmein - ',getKey('jasmein'))

              console.log('Jhon - ',getKey('jhon'))








              share|improve this answer




























                1














                Though late and other great answers already exist, still you can give below "..." and "Array.find" a try






                let people = new Map();
                people.set('1', 'jhon');
                people.set('2', 'jasmein');
                people.set('3', 'abdo');

                function getKey(value) {
                return [...people].find(([key, val]) => val == value)[0]
                }

                console.log('Jasmein - ',getKey('jasmein'))

                console.log('Jhon - ',getKey('jhon'))








                share|improve this answer


























                  1












                  1








                  1







                  Though late and other great answers already exist, still you can give below "..." and "Array.find" a try






                  let people = new Map();
                  people.set('1', 'jhon');
                  people.set('2', 'jasmein');
                  people.set('3', 'abdo');

                  function getKey(value) {
                  return [...people].find(([key, val]) => val == value)[0]
                  }

                  console.log('Jasmein - ',getKey('jasmein'))

                  console.log('Jhon - ',getKey('jhon'))








                  share|improve this answer













                  Though late and other great answers already exist, still you can give below "..." and "Array.find" a try






                  let people = new Map();
                  people.set('1', 'jhon');
                  people.set('2', 'jasmein');
                  people.set('3', 'abdo');

                  function getKey(value) {
                  return [...people].find(([key, val]) => val == value)[0]
                  }

                  console.log('Jasmein - ',getKey('jasmein'))

                  console.log('Jhon - ',getKey('jhon'))








                  let people = new Map();
                  people.set('1', 'jhon');
                  people.set('2', 'jasmein');
                  people.set('3', 'abdo');

                  function getKey(value) {
                  return [...people].find(([key, val]) => val == value)[0]
                  }

                  console.log('Jasmein - ',getKey('jasmein'))

                  console.log('Jhon - ',getKey('jhon'))





                  let people = new Map();
                  people.set('1', 'jhon');
                  people.set('2', 'jasmein');
                  people.set('3', 'abdo');

                  function getKey(value) {
                  return [...people].find(([key, val]) => val == value)[0]
                  }

                  console.log('Jasmein - ',getKey('jasmein'))

                  console.log('Jhon - ',getKey('jhon'))






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 15 '18 at 6:39









                  Nitish NarangNitish Narang

                  2,9601815




                  2,9601815






























                      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%2f47135661%2fhow-to-get-a-key-in-a-javascript-map-by-its-value%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

                      Florida Star v. B. J. F.

                      Error while running script in elastic search , gateway timeout

                      Adding quotations to stringified JSON object values