Map and Set with javascript

Recently new revelations have been obtained with regards to two underutilized effects, map and set

Map

with let a = new Map () , a map is created, this is similar to an object but the order of the values are memorized in contrast to regular objects where the order can be somewhat prone to whimsy.

New items can be added like so

map1.set(‘a’, 1);

the first value is a key second is a value.

The main benefit of this is that the order is retained for for of loops

// if (!m.has(i)) return i;

.has is also an innate function that will determine if a value is actually in the object. .size will tell you how big the object is

Set

Similar to objects, they only contain unique values

nums = Array.from(new Set(nums));

const set1 = new Set();

set1.add(42);

set1.add(42);

set1.add(13);

console.log(set1[0], ‘nothing’)

for (const item of set1) {

console.log(item);

// expected output: 42

// expected output: 13

}

the console log is undefined in this case. No keys it seems for this object. It is not an array

--

--

--

Starting a tech bootcamp

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to add email functionality to your React app through Email.js

AngularJS Interview Questions

JavaScript Chatbot in 10 Minutes

React | Best Practices

How to Assign an Avatar and Shield Based on Username in React

JavaScript For Beginners: the ‘new’ operator

How I Created A Twitter Bot with Node.js

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Jesse Chung

Jesse Chung

Starting a tech bootcamp

More from Medium

js class 2

ReactJS Basics

CREATE REACT APP : A SIMPLIFIED FOLDER STRUCTURE EXPLANATION FOR BEGINNERS.

Single Page Applications — Mixing a Rails backend with a JavaScript frontend