0
What is the difference between using Set() and array? Does this do the same or it depends on what you want to use them for?
I'm asking because I'm seeing a little bit of similarities. #Newbie For example: ```let arr = [] arr.push(1,2,3,4,5)``` And ... ```let arr = new Set() arr.add(1).add(2).add(3).add(4).add(5) for (let i of arr.values()) { console.log(i) }```
2 Answers
0
Besides the interface wich differ from one to another, the main difference is that you can store multiple times a v8alue in an Array, while a Set store uniques values...
For example:
var v, a = [], s = new Set(), t ='';
a.push(1,2,3,2,5);
s.add(1).add(2).add(3).add(2).add(4);
for (v of a) t += (t ? ',' : '')+v;
console.log('a:',t);
t = '';
for (v of s.values()) t += (t ? ',' : '')+v;
console.log('s:',t);
- 1
In map, you can refer to a value using the keys and you can perform actions on it using keys.