Skip to content

Instantly share code, notes, and snippets.

@unostar
Last active January 23, 2022 14:47
Show Gist options
  • Save unostar/3c38c9cfaf406b0119f45f73fddfaf1d to your computer and use it in GitHub Desktop.
Save unostar/3c38c9cfaf406b0119f45f73fddfaf1d to your computer and use it in GitHub Desktop.
Filter out multi-dimensional arrays
console.time("filter");
let cat = ([].concat.apply([], this.transits.map(item => item.categories))).filter((value, index, self) => self.indexOf(value) === index);
console.timeEnd("filter");
console.time("set");
let cat2 = [...new Set([].concat.apply([], this.transits.map(item => item.categories)))];
console.timeEnd("set");
console.time("flat")
let cat3 = [...new Set(this.transits.map(item => item.categories).flat())];
console.timeEnd("flat")
console.log(cat, cat2, cat3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment