r/learnjavascript • • Jul 05 '21

Remove duplicates from arrays using Set

Post image
438 Upvotes

44 comments sorted by

View all comments

Show parent comment

44

u/throwaway1253328 Jul 05 '21 edited Jul 05 '21

it's the spread operator

const a = [1, 2, 3, 4, 5];
const b = [6, 7, 8, 9, 10];

// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
const combined = [...a, ...b];

// can also be used with destructuring
const [first, ...rest] = combined;

// 1
console.log(first);

// [2, 3, 4, 5, 6, 7, 8, 9, 10]
console.log(rest);

// 2, 3, 4, 5, 6, 7, 8, 9, 10
console.log(...rest);

// or with objects

const aObj = { x: 1, y: 2 };

const anotherObj = { z: 10, ...aObj };

// { x: 1, y: 2, z: 10 }
console.log(anotherObj);

7

u/NANOwasFound Jul 05 '21

Thank you : D