Search code examples
javascriptarraysmapskeydistinct-values

How to convert Map key values into array in JavaScript


I have a Map that contains keys and their value. I want to convert all keyvalues into an array

const gameEvents = new Map([
  [17, '⚽ GOAL'],
  [36, '🔁 Substitution'],
  [47, '⚽ GOAL'],
  [61, '🔁 Substitution'],
  [64, '🔶 Yellow card'],
  [69, '🔴 Red card'],
  [70, '🔁 Substitution'],
  [72, '🔁 Substitution'],
  [76, '⚽ GOAL'],
  [80, '⚽ GOAL'],
  [92, '🔶 Yellow card'],
]);

I want that my new array should look like this

['⚽ GOAL','🔁 Substitution','⚽ GOAL' ,'🔁 Substitution', '🔶 Yellow card', '🔴 Red card', '🔁 Substitution','🔁 Substitution',, '⚽ GOAL', '⚽ GOAL', '🔶 Yellow card']


Solution

  • this will do

    const gameEvents = new Map([
      [17, '⚽ GOAL'],
      [36, '🔁 Substitution'],
      [47, '⚽ GOAL'],
      [61, '🔁 Substitution'],
      [64, '🔶 Yellow card'],
      [69, '🔴 Red card'],
      [70, '🔁 Substitution'],
      [72, '🔁 Substitution'],
      [76, '⚽ GOAL'],
      [80, '⚽ GOAL'],
      [92, '🔶 Yellow card'],
    ]);
    
    console.log([...gameEvents.values()]);