Skip to content

Instantly share code, notes, and snippets.

@marcelo-ribeiro
Last active June 17, 2020 03:06
Show Gist options
  • Save marcelo-ribeiro/04a103d02788827a385283af0e8cc36d to your computer and use it in GitHub Desktop.
Save marcelo-ribeiro/04a103d02788827a385283af0e8cc36d to your computer and use it in GitHub Desktop.
Create Unique Array, Remove Array Duplicates
const products = [
{id: 1, title: "Trim Dress", category: "women", collection: ["new", "featured"]},
{id: 2, title: "Belted Dress", category: "women", collection: ["featured"]},
{id: 3, title: "Fitted Dress", category: "men", collection: ["new"]}
];
const categories = new Set(
products.map(product => product.category)
);
// Created using inner array
const collection = new Set(
...products.map(product =>
product.collection.map(item => item)
)
);
console.log([...categories]);
// ["women", "men"]
console.log([...collection]);
// ["new", "featured"]
const numeros = [2,3,4,4,2,3,3,4,4,5,5,6,6,7,5,32,3,4,5];
console.log([...new Set(numeros)]);
// [2, 3, 4, 5, 6, 7, 32]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment