Skip to content

Instantly share code, notes, and snippets.

@tsertkov
Last active October 22, 2019 09:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsertkov/9ba84b56ac425a708b5159e5bba76aeb to your computer and use it in GitHub Desktop.
Save tsertkov/9ba84b56ac425a708b5159e5bba76aeb to your computer and use it in GitHub Desktop.
JavaScript array tips & tricks

JavaScript array tips & tricks

// Array mapping with .from()

Array.from([{ 'key': 1, 'value': 'val1'}, { 'key': 2, 'value': 'val2' }], ({key}) => key)
Array.from([{ 'key': 1, 'value': 'val1'}, { 'key': 2, 'value': 'val2' }], ({value}) => value)

// Get random value from array

const a = ['blue', 'yellow']
a[(Math.floor(Math.random() * (a.length)))]

// Arrays intersection

const a1 = [0, 2, 4, 6, 8, 8]
const a2 = [1, 2, 3, 4, 5, 6]
[…new Set(a1)].filter(item => a2.includes(item))

// Unique elements

const a = [0, 1, 2, 1, 1, 0]
Array.from(new Set(a))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment