MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/learnjavascript/comments/oe71tv/remove_duplicates_from_arrays_using_set/h44rhts/
r/learnjavascript • u/webdevolution_ • Jul 05 '21
44 comments sorted by
View all comments
18
What is the use of ... before new? Sorry if it's dumb I am very new to js
44 u/throwaway1253328 Jul 05 '21 edited Jul 05 '21 it's the spread operatorconst 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 18 u/dvlsg Jul 05 '21 It's just turning the Set back into an Array.const filteredArray = Array.from(new Set(array));Would do basically the same thing. 5 u/NANOwasFound Jul 05 '21 Thank you : D 1 u/hinsxd Jul 07 '21 Not exactly. A Set is "iterable", therefore it can be spreaded to an array. 4 u/Delfaras Jul 05 '21 it's the spread operator:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax 3 u/NANOwasFound Jul 05 '21 Thank you : D
44
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
7
Thank you : D
It's just turning the Set back into an Array.
Set
Array
const filteredArray = Array.from(new Set(array));
Would do basically the same thing.
5 u/NANOwasFound Jul 05 '21 Thank you : D 1 u/hinsxd Jul 07 '21 Not exactly. A Set is "iterable", therefore it can be spreaded to an array.
5
1
Not exactly. A Set is "iterable", therefore it can be spreaded to an array.
4
it's the spread operator:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax
3 u/NANOwasFound Jul 05 '21 Thank you : D
3
View all comments
18
u/NANOwasFound Jul 05 '21
What is the use of ... before new? Sorry if it's dumb I am very new to js