While writing the blog on Array method, I have came across some topics that are new to me and Map and Set are one of them.
If youβd like to explore them too π
If You Donβt Know These Array Methods, We Need to Talk.
Kunal γ» Mar 3
So in this blog we will cover :
Topics to Cover
- Before Map & Set (The Old Way)
- What Map is
- Difference between Map and Object
- What Set is
- Difference between Set and Array
- When to use Map and Set
Before Map & Set
Before learning Set and Map , I tried doing things in the old ways using array and objects.
- For Map we do
const data = {};
data["name"] = "Kunal";
data["age"] = 21;
console.log(data["name"]); // Kunal
- For Set we do
const arr = [1,2,2,3,3,5]
const unique = []
for (let i = 0; i < arr.length; i++) {
if(!unique.includes(arr[i])){
unique.push(arr[i])
}
}
console.log(unique); // [1,2,3,5]
It worked but it required more code and wasn't clean or efficient.
That's when i come across Set and Map.
Map
The Map object stores data in a key/value pair structure just like an object. The main difference between a regular object and a Map are:
- A
Mapcan have any type of data as key Value - A
Mapmaintains the order of the data
How to create a Map object
To create a Map object you can call the Map() constructor as follows :
const newMap = new Map()
Like objects, Map is not just data, it comes with powerful built-in methods and properties.
Map Object Methods and Properties
A Map object has the following methods and properties :
Donβt just read try each method and property yourself.
Here, Iβm only covering some important ones of the Map object.
- To insert data into the Map object you can use
set()method.
const myMap = new Map()
myMap.set(1 , "like this blog")
myMap.set(2 , "hello world")
console.log(myMap)
The output of the above code
Map(2) { 1 => 'like this blog', 2 => 'hello world' }
- To retrieve a value from the Map object you can use the
get()method
console.log(myMap.get(1))
You have to pass the key as its argument
- To see if a certain key exist in a Map object we can use
has()method
myMap.has(1) // true
Now Lets see the main difference between Map and the object
Difference between Map and Object
| Feature | Object π§± | Map πΊοΈ |
|---|---|---|
| Key Type | String / Symbol only | Any type (object, number, etc.) |
| Order | Not strictly guaranteed* | Maintains insertion order |
| Size | No direct property |
size property |
| Iteration | Not directly iterable | Directly iterable |
| Use Case | Simple/static data | Dynamic & frequent updates |
Set
The Set object allows you to store a collection of element just like an array. The difference between a Set and array are :
- A Set require all elements to be unique
- A Set has fewer methods for data manipulation
How to create a Set object
To create a new Set object you need yo call a Set() constructor as follows :
const mySet = new Set()
Like array, Set is not just data, it comes with powerful built-in methods and properties.
Set Object Methods and Properties
Like Map here i only cover some important methods and property rest you can do it your own.
- To add an element to the
Setobject, you can use theadd()method:
const mySet = new Set()
mySet.add(1)
mySet.add(2)
mySet.add(3)
console.log(mySet) // Set(3) { 1, 2, 3 }
- To check if the Set has a specific value, use the
has()method:
mySet.has('Jack'); // true
mySet.has('Michael'); // false
- To check the number of Items in Set we use
size()method:
console.log(mySet.size) // 3
Let see the Difference between
ArrayandSet
Difference between Set and Array
| Feature | Array π | Set π’ |
|---|---|---|
| Duplicates | Allowed | Not allowed (unique only) |
| Order | Maintains order | Maintains insertion order |
| Access | By index (arr[0]) |
No index access |
| Use Case | List of items | Unique collection |
| Methods Focus |
push(), pop()
|
add(), has()
|
When to use Map
- You need key β value relationship
- Keys are not just strings (can be objects, numbers)
- You want fast lookup by key
Example: userId β userData
When to use Set
- You only care about unique values
- You want to remove duplicates
- You just need to check existence
Example: unique tags, unique IDs
Thanks for reading ! if enjoyed this blog , you can read more on this π
Top comments (0)