dev-resources.site
for different kinds of informations.
Sets
Published at
8/25/2024
Categories
webdev
javascript
es6
datastructures
Author
mahf001
Main Article
Author
7 person written this
mahf001
open
- Before ES6, JS had only two inbuilt Data Structures namely arrays & objects. ES6 introduced two new DS: Sets, Map.
- For storage & orderly retirieveal use arrays.
- Use sets only if order doesn't matter, and you just need to check the presence of element inside the data structure.
- Sets are not intended to replace arrays. Arrays are more important.
- Whenever values need to be stored in order along with duplicates, arrays have to be used.
## Usecase: To remove duplicate values of arrays.
const order = ['pizza','burger','pasta','noodles','pizza','noodles','burger'];
const items = new Set(order);
items; // All duplicate values are removed
const city = new Set("California").size;
city; // 8
Sets:
- Sets are also iterables like String.
- Collection of unique values.
const city = new Set("California");
city; // Set(8) { 'C', 'a', 'l', 'i', 'f', 'o', 'r', 'n' }
## Difference between Set & Array:
1. Although looks similar to array, but it has no key-value pairs. Hence, set[0] is invalid.
2. Only a list of unique values, all duplicate removed.
3. Order of element is irrelevant
## Similarities between Arrays & Sets:
1. Set has size property, Array has length propery.
2. Set has 'has' method, Array has includes method.
const order = ['pizza','burger','pasta','noodles','pizza','noodles','burger'];
const items = new Set(order);
items; // Set(4) { 'pizza', 'burger', 'pasta', 'noodles' }
//Both array and sets are iterables. Hence easier to convert from sets to array.
[...items];
Adv: Can never have duplicate, although can hold mixed data types.
Most common iterable is Array. Ex. Syntax: new Set(iterable)
List of methods & properties on Sets:
.size; // returns a numerical value
.has('name'); // returns a boolean value
.add('name'); // returns the set with added value
.delete('name'); // returns a boolean value
.clear(); // deletes all elements. returns Set(0) {}
es6 Article's
30 articles in total
Next-Generation Buttons: Implementing the Command Pattern through Web Components
read article
Hoisting: facing Temporal dead zone
read article
Learn javascript promises. Part 1 β What is a promise?
read article
Bootcamping 02: Named exports and default exports - does it really matter?
read article
Mastering Modern JavaScript: A Deep Dive into ES6 Function Creation and Best Practices
read article
Promises: The Ability to Write Asynchronous Code Using Javascript
read article
Exploring JavaScript's Modern Primitives: BigInt and Symbol
read article
JavaScript ES6 Release Notes: Unleashing the Power of Modern JavaScript
read article
WHY YOU SHOULD LEARN ES6
read article
Understanding ES6 API's
read article
Transpiler vs Polyfills
read article
JavaScript Spread Syntax: Expanding Arrays and Objects
read article
API Design and Debugging:A Comprehensive Guide for Beginersπ
read article
Understanding the JavaScript Spread Operator (With Examples)
read article
A Comprehensive Guide to ES6 and Arrow Functions
read article
Controla tus promesa con JavaScript
read article
Sets
currently reading
Enhanced Object Literals
read article
Iteration Stament i.e for-of loop
read article
1.1 Ins & outs of ES6(JavaScript) Import with Realworld Example and Demo Project.
read article
Math Namespace & BigInt
read article
JavaScript - Destructuring Arrays & Objects [Live Doc]
read article
ES2015 (ES6) Tips, Tricks, Best Practices, and Code Snippet Examples for Your Day-to-Day Workflow
read article
Objects in JavaScript
read article
Intro to DSA & Big O Notation
read article
Execution Context & Call Stack
read article
Asynchronous programming in Javascript - Callbacks, Promises & Async Await
read article
Loops in JavaScript !!ππ
read article
Array
read article
Functions
read article
Featured ones: