Skip to content

Instantly share code, notes, and snippets.

@djD-REK
Last active August 14, 2019 21:09
Show Gist options
  • Save djD-REK/fc2066eaff6c446a510d0120469f1348 to your computer and use it in GitHub Desktop.
Save djD-REK/fc2066eaff6c446a510d0120469f1348 to your computer and use it in GitHub Desktop.
const suits = ["♠️", "♣️", "♥️", "♦️", "🃏"];
const ranks = ["A", "K", "Q", "J", "🃏"];
console.log(suits.map((suit, index) => [ranks[index], suit]));
// result: [["A", "♠️"], ["K", "♣️"], ["Q", "♥️"], ["J", "♦️"], ["🃏", "🃏"]]
console.log(suits.flatMap((suit, index) => [ranks[index], suit]));
// result: ["A", "♠️", "K", "♣️", "Q", "♥️", "J", "♦️", "🃏", "🃏"]
// If for some reason you needed to use a depth > 1 for flat, you can't use flatMap
console.log(suits.map((suit, index) => [ranks[index], suit]).flat(Infinity));
// result: ["A", "♠️", "K", "♣️", "Q", "♥️", "J", "♦️", "🃏", "🃏"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment