Day 10 0f #30DaysOfJavaScript

Day 10 0f #30DaysOfJavaScript

Table of contents

Introduction

Hey folks, I hope that you must have read my Day 9 Article on #30DaysOfJs. In case, if you haven't then make sure you give it a read by clicking here.

Set

A Set is a collection of elements. It can only contain unique elements. Let us see how to create a set in the section below.

  • Creating an Empty Set-

For Example:

const companies = new Set()
console.log(companies);

// Output --> Set(0) {}
  • Creating Set From an Array-

For Example 1:

const languages = [
  'English',
  'Finnish',
  'English',
  'French',
  'Spanish',
  'English',
  'French',
]

const setOfLanguages = new Set(languages);
console.log(setOfLanguages);

// Output --> Set(4) {"English", "Finnish", "French", "Spanish"}

Set is an iterable form of Object and we can iterate through various elements.

For Example 2:

const languages = [
  'English',
  'Finnish',
  'English',
  'French',
  'Spanish',
  'English',
  'French',
]

const setOfLanguages = new Set(languages)

for (const language of setOfLanguages) {
  console.log(language);
}

// Output -->   
// English
// Finnish
// French
// Spanish
  • Adding an Element To a Set-

For Example:

const companies = new Set() // Creating an empty set
console.log(companies.size) // 0

companies.add('Google') // Adding an element to the set
companies.add('Facebook')
companies.add('Amazon')
companies.add('Oracle')
companies.add('Microsoft')
console.log(companies.size) // 5 elements in the set
console.log(companies)

// Output --> Set(5) {"Google", "Facebook", "Amazon", "Oracle", "Microsoft"}
  • Deleting an Element From a Set-

For Example:

console.log(companies.delete('Google'))
console.log(companies.size) // 4 elements left in the set
  • Checking an Element In The Set-

For Example:

console.log(companies.has('Apple')) // false
console.log(companies.has('Facebook')) // true
  • Clearing The Set- Used for removing all the elements from a set.

For Example:

companies.clear()
console.log(companies)
  • Union Of Sets- To find a union of two sets can be achieved using the spread operator. Let's find the union of set A and set B (A U B).

For Example:

let a = [1, 2, 3, 4, 5]
let b = [3, 4, 5, 6]
let c = [...a, ...b]

let A = new Set(a)
let B = new Set(b)
let C = new Set(c)

console.log(C);

// Output --> Set(6) {1, 2, 3, 4, 5,6}
  • Intersection Of Sets- To find an intersection of two sets can be achieved using the filter. Let's find the intersection of set A and set B (A ∩ B).

For Example:

let a = [1, 2, 3, 4, 5]
let b = [3, 4, 5, 6]

let A = new Set(a)
let B = new Set(b)

let c = a.filter((num) => B.has(num))
let C = new Set(c)

console.log(C);

// Output --> Set(3) {3, 4, 5}
  • Difference Of Sets- To find the difference between two sets can be achieved using the filter. Let's find the difference between set A and set B (A \ B).

For Example:

let a = [1, 2, 3, 4, 5]
let b = [3, 4, 5, 6]

let A = new Set(a)
let B = new Set(b)

let c = a.filter((num) => !B.has(num))
let C = new Set(c)

console.log(C);

// Output --> Set(2) {1, 2}

Map

  • Creating an Empty Map-

For Example:

const map = new Map()
console.log(map);

// Output --> Map(0) {}
  • Creating a Map From an Array-

For Example:

countries = [
  ['Finland', 'Helsinki'],
  ['Sweden', 'Stockholm'],
  ['Norway', 'Oslo'],
]
const map = new Map(countries)
console.log(map);
console.log(map.size);

// Output --> Map(3) {"Finland" => "Helsinki", "Sweden" => "Stockholm", "Norway" => "Oslo"} 
// 3
  • Adding Values To The Map-
const countriesMap = new Map()
console.log(countriesMap.size) // 0
countriesMap.set('Finland', 'Helsinki')
countriesMap.set('Sweden', 'Stockholm')
countriesMap.set('Norway', 'Oslo')
console.log(countriesMap);
console.log(countriesMap.size);

// Output --> Map(3) {"Finland" => "Helsinki", "Sweden" => "Stockholm", "Norway" => "Oslo"}
// 3
  • Getting a Value From Map-
console.log(countriesMap.get('Finland'));

// Output -->
// Helsinki
  • Checking Key In The Map- Used to check if a key exists in the map using has method. It returns true or false.
console.log(countriesMap.has('Finland'));

// Output -->
// true
  • Getting the Values From Map Using Loop-

For Example 1:

for (const country of countriesMap) {
  console.log(country)
}

// Output -->
// (2) ["Finland", "Helsinki"]
// (2) ["Sweden", "Stockholm"]
// (2) ["Norway", "Oslo"]

For Example 2:

for (const [country, city] of countriesMap){
 console.log(country, city)
}

// Output -->
// Finland Helsinki
// Sweden Stockholm
// Norway Oslo

I hope that you must have found this article quite helpful. If yes, then do give a read to some of my other articles!

Who knows you might become a great programmer 🤔!