簡體   English   中英

如何將 Map 鍵值轉換為 JavaScript 中的數組

[英]How to convert Map key values into array in JavaScript

我有一個包含鍵及其值的 Map。 我想將所有鍵值轉換為數組

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'],
]);

我希望我的新數組看起來像這樣

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

這會做

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

嘗試這個:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM