繁体   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