Set Vs WeakSet

SET

				
					//************************SET***********************************************

//1.They allow you to store unique values
//2.sets will be created using constructor
//3.sets you can add primitive and reference data types

//1-------------They allow you to store unique values-----------------------
const set = new Set();
set.add(1);
set.add(1);
set.add(3);
console.log(set); //Set(2) {1, 3}
set.add(4).add(5); //Set(2) {1, 3, 4 ,5}

//---------------sets you can add primitive and reference data types ----
const set3 = new Set();
set3.add({ name: "anurag" });
set3.add({ name: "anurag" });
set3.add({ name: "abhishek" });
set3.add(1);
set3.add([12, 13]);
set3.add([12, 13]);
set3.add(3);
set3.add(3);
set3.add("TEST");
set3.add("TEST");
set3.add(true);
set3.add(true);
console.log("set3", set3);

// IT DOESN'T STORE UNIQUE VALUES WHEN IT COMES TO OBJECTS AND ARRAYS AS IN ABOVE EXAMPLE
// YOU WILL SEE OUTPUT HAVE 2 SAME OBJECT REPEATED TWICE
// YOU WILL SEE SAME ARRAY REPEATED TWICE
// UNIQUENESS ONLY FOR PRIMITIVE DATA TYPES

//OUTPUT
/*
Set(9) {{…}, {…}, {…}, 1, Array(2), …}
[[Entries]]
0: Object
1: Object
2: Object
3: 1
4: Array(2)
5: Array(2)
6: 3
7: "TEST"
8: true
size: 9
*/
console.log(set3.size); //9
console.log(set3.delete(3)); //8 --WORKS
console.log("set3", set3);
console.log("set3 -> has 1??::->", set3.has(1)); //TRUE

//-->BOTH ARE FALSE -->FOR ARRAY AND OBJECTS
//-->YOU CANNOT DELETE , YOU CANNOT USE HAS
console.log("set3 -> has [12, 13]??::->", set3.has([12, 13]));
console.log("set3 -> has??::->", set3.has({ name: "anurag" }));
console.log(set3.delete({ name: "anurag" }));

//<--YOU NEED TO LOOP -->DELETE WORKS
//-->YOU NEED TO LOOP -->HAS WORKS
set3.forEach((obj) => {
  console.log(obj);
  if (obj.name === "anurag") {
    if (set3.has(obj)) console.log("has works in this way");
    set3.delete(obj);
  }
});

console.log("set3 - object deleted::", set3);
console.log("set3 - size::", set3.size); //6

//-->ENTRIES --SETS ARE ITERABLE
for (let [key, value] of set3.entries()) {
  console.log(`the key is ${key}:: value is:: ${value}`);
}
//OUTPUT:-
// the key is [object Object]:: value is:: [object Object]
// the key is 1:: value is:: 1
// the key is 12,13:: value is:: 12,13
// the key is 12,13:: value is:: 12,13
// the key is TEST:: value is:: TEST
// the key is true:: value is:: true

console.log("set3 - clear::", set3.clear());
console.log("set3 - size::", set3.size); //0

//---------------Converting Array to set------------------------------------
const array = [1, 2, 3, 4, 4]; //array literal
const set1 = new Set(array);
console.log("set1", set1);
//output
// Set(4) {1, 2, 3, 4}
// [[Entries]]
// 0: 1
// 1: 2
// 2: 3
// 3: 4
// size: 4

//---------------Sets are used to remove duplicates from array--------->
let arr2 = [2, 2, 2, 2, 3, 4, 4, 5];
console.log([...new Set(arr2)]);
console.log(Array.from(new Set(arr2)));
//output- [2, 3, 4, 5]

//---------------new Set(obj4)-->This will throw error--------->
const obj4 = {
  firstname: "anurag",
  surname: "nayak",
};

//const set4 = new Set(obj4);
//Uncaught TypeError: object is not iterable (cannot read property Symbol(Symbol.iterator))

//YOU HAVE TO DO EITHER OF THESE
//1. .add
//2. or new Set([obj4]);
const set4 = new Set();
set4.add(obj4);

const set5 = new Set([obj4]);
set4.add(obj4);
				
			

WEAKSET

				
					//**********************************WEAK SETS********************************************/

//1.<--------Only add reference values...you cannot add primitive data--------->

const weakset1 = new WeakSet();
//weakset1.add(1); //Uncaught TypeError: Assignment to constant variable.

//2.<--------new Set(obj4)-->This will throw error----------------------------->
const obj2 = {
  firstname: "anurag",
  surname: "nayak",
};

//const weakset2 = new WeakSet(obj2);

//3.<---------Uncaught TypeError: weakset2 is not iterable--------------------->
const weakset2 = new WeakSet([obj2]);
for (let val of weakset2) {
  console.log("weakset2 val::", val);
}

//4.<---------weakset has fewer methods -> add delete has---------------------->
				
			

Guess The Output

				
					let wSet = new WeakSet();
let obj = { name: "Anurag" };
wSet.add(obj);
console.log(wSet.has(obj)); //true


obj = "I have become from object to string :) ";
console.log(wSet.has(obj)); //false


console.log(wSet);
/*
 WeakSet {{…}}
 [[Entries]]
 0:
 value: {name: 'Anurag'}
 [[Prototype]]: WeakSet
*/

				
			

Leave a Comment