Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions JavaScript_Advance/set-object.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/* Examples of using Sets */

const testSet = new Set([1, 2, "orange", { name: "Frank" }]);

// Instance Methods and Properties

console.log(testSet.size); // output: 4

testSet.add(4); // testSet [1, 2, "orange", {name: "Frank"}, 4]
testSet.add(4); // Value not added, it's duplicated
testSet.delete(2); // testSet [1, "orange", {name: "Frank"}, 4]
testSet.has(1); // Return true
testSet.has({ name: "Frank" }); // Return false, because the use of "==="
testSet.clear(); // testSet []

// Iterating Sets

const iterSet = new Set([1, "blue", 125, "right"]);

// Using for..of

for (let item of iterSet) console.log(item); // output: 1, "blue", 125, "right"
for (let item of iterSet.keys()) console.log(item); // same output
for (let item of iterSet.values()) console.log(item); // same output
for (let item of iterSet.entries()) console.log(item); // output: [1, 1], ["blue", "blue"], ...etc

// Using for..each

iterSet.forEach(value => console.log(value)); // output: 1, "blue", 125, "right"

// Creating a Set from an Array

const array = [1, 2, 3, "Alehop"];
const arrToSet = new Set(array);

console.log(arrToSet); // output: [1, 2, 3, "Alehop"]

// Deleting duplicated elements in an Array using Set and spread operator

const dupArray = [1, 2, 2, "yes", "no", "yes", 69, 420];
const notDupArray = [...new Set(dupArray)];

console.log(notDupArray); // output: [1, 2, "yes", "no", 69, 420]

// Creating a Set form an String

const text = "Nice";
const strToSet = new Set(text);

console.log(strToSet); // output: ["N", "i", "c", "e"]

// Deleting duplicated letters in a string

const dupText = "Niceeee";
const notDupText = [...new Set(dupText)].join("");

console.log(notDupText); // output: "Nice"

54 changes: 54 additions & 0 deletions docs/JavaScript_Advance/set-object.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Set object
The ***Set*** object stores a collection of values of any type, where each value can only occur once. Sets let you mix in different types of primitives and objects in the same
set, making a strickly equality test comparision when it's created and deleting duplicated elements. Set can also be iterated using **for..of** loops or **forEach()** method,
which iterate the values in insertion order.

## Creating Sets


The Set object can be created using the keyword ***new***:
```javascript
new Set([iterable]);
```

### Parameters
`iterable`
An iterable object. All the elements will be copied to the new created Set. If this parameter is not specify or it's value is null, the new Set is empty.

### Return value
A new Set object.


## Instance Methods


`set.add(value)`
*Adds* a new element to the Set with the given *value*. If the element is already contained in the Set, the element will be not added.

`set.delete(value)`
Delete the value in the Set. If the value exist return *true*, otherwise *false*.

`set.has(value)`
Return *true* if the value exists in the Set, else return *false*.

`set.clear()`
Delete all the elements in the Set. Return an *empty* set.

### Iteration Methods

`set.forEach(callback(value, key, set))`
Call ***callback*** for each element in the Set. ***Value*** and ***key*** parameters represents the current element being processes. The duplication of values is to mantain
compatibility with Map objects. The ***set*** argument represent the Set object which forEach() was called upon.

`set.entries()`
Return and interator with and array of **[value, value]** for each element, similar to Map object but with the same value for *value* and *key*.

`set.values()`
Return an interator that contains the values for each element in the Set object in insertion order.

`set.keys()`
The same as ***set.values()***.


## Examples
Examples can be consulted in the corresponding "***set-objects.js***" file.