TILs, snippets—my digital code garden 🌱. By Zander Martineau
All tags All notes javascript (47) css (33) react (32) html (13) typescript (9) testing (8) node (4) cli (4) graphql (4) devops (4) git (4) cheatsheet (4) storybook (2) typography (2) networking (1) animation (1) services (1) api (1) a11y (1) deno (1) cypress (1) vscode (1) 11ty (1) untagged (15)
© 2023 • Made by Zander • Colophon • RSS

❌ Remove array duplicates with Set

Link https://www.samanthaming.com/tidbits/43-3-ways-to-remove-array-duplicates
Note date 21 Jan, 2023
Tags javascript
On this page
  1. Using Set

Using Set Jump to heading

const array = ['🐑', 1, 2, '🐑', '🐑', 3]

// Step 1
const uniqueSet = new Set(array)
// Set { '🐑', 1, 2, 3 }

// Step 2
const backToArray = [...uniqueSet]
// ['🐑', 1, 2, 3]

// or Step 1
const uniqueSet = [...new Set(array)]
// ['🐑', 1, 2, 3]

← Back home