How to get a key in a JavaScript Map by its value?
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
add a comment |
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
13
Then why did you store it the wrong way round? :-P
– Bergi
Nov 6 '17 at 16:06
add a comment |
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
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
javascript dictionary
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
add a comment |
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
add a comment |
4 Answers
4
active
oldest
votes
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.
@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 ofArray.from
with a mapping of the value.
– Nina Scholz
Sep 12 '18 at 20:49
|
show 5 more comments
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'))
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 thevar
to aconst
..
– Keith
Nov 6 '17 at 13:15
@Keith Thanks for pointing it out. Have updated it tolet
.
– Rajesh
Nov 6 '17 at 13:22
1
Nice one,. just a heads upconst
works here too infor of
. It's doesn't work like a normalfor
in this regard.
– Keith
Nov 6 '17 at 13:28
1
Just my personal preference to uselet
overconst
.
– Rajesh
Nov 6 '17 at 13:38
You don't need themap.entries()
, the map itself acts as an iterator. I submitted an edit.
– philraj
Sep 12 '18 at 20:44
add a comment |
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.
add a comment |
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'))
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
@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 ofArray.from
with a mapping of the value.
– Nina Scholz
Sep 12 '18 at 20:49
|
show 5 more comments
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.
@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 ofArray.from
with a mapping of the value.
– Nina Scholz
Sep 12 '18 at 20:49
|
show 5 more comments
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.
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.
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 ofArray.from
with a mapping of the value.
– Nina Scholz
Sep 12 '18 at 20:49
|
show 5 more comments
@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 ofArray.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
|
show 5 more comments
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'))
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 thevar
to aconst
..
– Keith
Nov 6 '17 at 13:15
@Keith Thanks for pointing it out. Have updated it tolet
.
– Rajesh
Nov 6 '17 at 13:22
1
Nice one,. just a heads upconst
works here too infor of
. It's doesn't work like a normalfor
in this regard.
– Keith
Nov 6 '17 at 13:28
1
Just my personal preference to uselet
overconst
.
– Rajesh
Nov 6 '17 at 13:38
You don't need themap.entries()
, the map itself acts as an iterator. I submitted an edit.
– philraj
Sep 12 '18 at 20:44
add a comment |
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'))
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 thevar
to aconst
..
– Keith
Nov 6 '17 at 13:15
@Keith Thanks for pointing it out. Have updated it tolet
.
– Rajesh
Nov 6 '17 at 13:22
1
Nice one,. just a heads upconst
works here too infor of
. It's doesn't work like a normalfor
in this regard.
– Keith
Nov 6 '17 at 13:28
1
Just my personal preference to uselet
overconst
.
– Rajesh
Nov 6 '17 at 13:38
You don't need themap.entries()
, the map itself acts as an iterator. I submitted an edit.
– philraj
Sep 12 '18 at 20:44
add a comment |
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'))
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'))
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 thevar
to aconst
..
– Keith
Nov 6 '17 at 13:15
@Keith Thanks for pointing it out. Have updated it tolet
.
– Rajesh
Nov 6 '17 at 13:22
1
Nice one,. just a heads upconst
works here too infor of
. It's doesn't work like a normalfor
in this regard.
– Keith
Nov 6 '17 at 13:28
1
Just my personal preference to uselet
overconst
.
– Rajesh
Nov 6 '17 at 13:38
You don't need themap.entries()
, the map itself acts as an iterator. I submitted an edit.
– philraj
Sep 12 '18 at 20:44
add a comment |
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 thevar
to aconst
..
– Keith
Nov 6 '17 at 13:15
@Keith Thanks for pointing it out. Have updated it tolet
.
– Rajesh
Nov 6 '17 at 13:22
1
Nice one,. just a heads upconst
works here too infor of
. It's doesn't work like a normalfor
in this regard.
– Keith
Nov 6 '17 at 13:28
1
Just my personal preference to uselet
overconst
.
– Rajesh
Nov 6 '17 at 13:38
You don't need themap.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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 6 '17 at 15:36
David SpillettDavid Spillett
1,050917
1,050917
add a comment |
add a comment |
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'))
add a comment |
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'))
add a comment |
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'))
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'))
answered Nov 15 '18 at 6:39
Nitish NarangNitish Narang
2,9601815
2,9601815
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
13
Then why did you store it the wrong way round? :-P
– Bergi
Nov 6 '17 at 16:06