Skip to content

Instantly share code, notes, and snippets.

@kironroy
Created May 23, 2023 01:49
Show Gist options
  • Save kironroy/6d519444ba3d59cc87fc759d0f611876 to your computer and use it in GitHub Desktop.
Save kironroy/6d519444ba3d59cc87fc759d0f611876 to your computer and use it in GitHub Desktop.
map iteration js
console.log('----------new map--------------');
const question = new Map([
['question', 'What is the best programming language in the world'],
[1, 'C'],
[2, 'Java'],
[3, 'JavaScript'],
['correct', 3],
[true, 'Correct 😊'],
[false, 'try again'],
]);
console.log(question);
console.log(question.get('question'));
for (const [key, value] of question) {
if (typeof key === 'number') {
console.log(`Answer ${key}: ${value}`);
}
}
// const answer = Number(prompt('Your answer'));
// console.log(answer);
// console.log(question.get(question.get('correct') === answer));
// convert map to array
console.log([...question]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment