Skip to main content

Command Palette

Search for a command to run...

[JavaScript] forEach with Maps and Sets

Published
•View as Markdown
[JavaScript] forEach with Maps and Sets
// Maps
const currencies = new Map([
  ['USD', 'United States dollar'],
  ['EUR', 'Euro'],
  ['GBP', 'Pound sterling'],
]);

currencies.forEach(function (value, key, map) {
  console.log(`${key}: ${value}`);
});

//Sets
const currenciesUnique = new Set(['USD', 'GBP', 'USD', 'EUR', 'EUR']);
console.log(currenciesUnique);
currenciesUnique.forEach(function (value, _, map) {
  console.log(`${value}: ${value}`);
});

forEach with maps and sets.PNG

Set does not have any key or index here.

JavaScript

Part 1 of 50

More from this blog

Jayden's Blog

166 posts