1

I have a map object and I would like to get the value of a specific key ( the value of key it self ) from the Map object let say we want to get 'correct'

we can get the values of 'correct' key by : question.get('correct') // return 3 but i want : someCode //return 'correct'

const question = new Map();

question.set('question','What is the latest version of javasript ?')
question.set(1,'es4')
question.set(2,'es5')
question.set(3,'es6')
question.set('correct',3)
question.set(true , 'correct Answer');
question.set(false , 'wrong Answer')

Hakim Bencella
  • 187
  • 4
  • 12

3 Answers3

1

You can get the values of keys as follows which will get all the keys of the map object:

question.keys();
sda87
  • 372
  • 3
  • 10
0

If your use case is just to test the existence of key then simply use has, but if you want to get key back if it present else some other value then you can use has to test key is present or not, here getKey function check if key is present on Map return that key else return Not found

const question = new Map();

question.set('question','What is the latest version of javasript ?')
question.set(1,'es4')
question.set(2,'es5')
question.set(3,'es6')
question.set('correct',3)
question.set(true , 'correct Answer');
question.set(false , 'wrong Answer')

let getKey = key => question.has(key) ? key : 'Not found'

console.log(getKey('correct'))
console.log(getKey('randome key'))

You can even use [...Map.keys()] to get array of keys and then iterator over and find if the value is found or not

Code Maniac
  • 37,143
  • 5
  • 39
  • 60
  • i want something like that : if ( valueOfKey === userInput ) { do somethings } – Hakim Bencella Sep 19 '19 at 01:21
  • @HakimBencella what do you mean by valueOfKey, `question.get('counter')` is giving you value of that key and you didn't wanted that right ?, if you just need to check what use passed is present in map or not just use `if(question.has(userInput)){ do something }` – Code Maniac Sep 19 '19 at 01:23
  • No , lets say i want `if ( userInput === 2 ) { do something }` just the key '2' and not all of them – Hakim Bencella Sep 19 '19 at 01:28
  • @HakimBencella `if(question.has(userInput)){ do something }` this will do the same what you're saying – Code Maniac Sep 19 '19 at 01:36
  • `if(question.has(userInput)){ do something }` if the user input is '3' it return true and i want it to return true only if the user input = '2' and not '3' .. – Hakim Bencella Sep 19 '19 at 01:43
0

To get a key based on a value, you can iterate over map entries using Map.entries() and return the key if found.

const question = new Map();

question.set('question','What is the latest version of javasript ?');
question.set(1,'es4');
question.set(2,'es5');
question.set(3,'es6');
question.set('correct',3);
question.set(true , 'correct Answer');
question.set(false , 'wrong Answer');

function getKey(map, input) {
  for (let [key, value] of map.entries()) {
     if (value === input) {
       return key;
     }
  }
  
  return "Not found";
}

console.log(getKey(question, 3));
console.log(getKey(question, 2));
Nikhil
  • 6,493
  • 10
  • 31
  • 68