简体   繁体   中英

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']

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()]);

Try this:

 const gameEvents = [ [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.map(x => x[1]))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM