DEV Community

Discussion on: Sets in JavaScript

Collapse
 
karataev profile image
Eugene Karataev

Is it correct to say that Set is like array, but with unique items only and Map is like object where keys might be of any type (not limited to strings and symbols)?

Collapse
 
attacomsian profile image
Atta

Well Set might look like an array but there are some notable differences. Apart from unique values, sets have different ways for initializing, accessing / adding / removing values.

Collapse
 
perpetualwar profile image
Srđan Međo

Sets are also unordered lists of elements, unlike arrays.

Thread Thread
 
karataev profile image
Eugene Karataev

Why? We can use forEach method on Sets to iterate the collection by insertion order.

Thread Thread
 
perpetualwar profile image
Srđan Međo

Correct. Iteration is possible in the insertion order, but items are not indexed and they cannot be sorted unless converted to array beforehand. Maybe worth pointing out.

Thread Thread
 
karataev profile image
Eugene Karataev

Agree. Sets also don't have array methods like map/filter/reduce and others. I just don't use Set/Map in my daily work and always forget what data structure is used for. By comparing Set to Array and Map to Object I want to create a mental connection in my head to remember the purpose of these data structures without looking in documentation.