parent
56473b129c
commit
50e26e4fe2
4 changed files with 103 additions and 0 deletions
@ -0,0 +1,55 @@ |
||||
There are serveral ways to find unique values in an array. Here are some of them: |
||||
|
||||
## Using `Set` |
||||
|
||||
```js |
||||
const roadmaps = ['JavaScript', 'React', 'Node.js', 'Node.js', 'JavaScript']; |
||||
const uniqueRoadmaps = [...new Set(roadmaps)]; |
||||
console.log(uniqueRoadmaps); // ['JavaScript', 'React', 'Node.js'] |
||||
``` |
||||
|
||||
## Using `filter()` |
||||
|
||||
```js |
||||
const roadmaps = ['JavaScript', 'React', 'Node.js', 'Node.js', 'JavaScript']; |
||||
const uniqueRoadmaps = roadmaps.filter( |
||||
(roadmap, index) => roadmaps.indexOf(roadmap) === index |
||||
); |
||||
console.log(uniqueRoadmaps); // ['JavaScript', 'React', 'Node.js'] |
||||
``` |
||||
|
||||
## Using `reduce()` |
||||
|
||||
```js |
||||
const roadmaps = ['JavaScript', 'React', 'Node.js', 'Node.js', 'JavaScript']; |
||||
const uniqueRoadmaps = roadmaps.reduce((unique, roadmap) => { |
||||
return unique.includes(roadmap) ? unique : [...unique, roadmap]; |
||||
}, []); |
||||
console.log(uniqueRoadmaps); // ['JavaScript', 'React', 'Node.js'] |
||||
``` |
||||
|
||||
## Using `forEach()` |
||||
|
||||
```js |
||||
const roadmaps = ['JavaScript', 'React', 'Node.js', 'Node.js', 'JavaScript']; |
||||
const uniqueRoadmaps = []; |
||||
roadmaps.forEach((roadmap) => { |
||||
if (!uniqueRoadmaps.includes(roadmap)) { |
||||
uniqueRoadmaps.push(roadmap); |
||||
} |
||||
}); |
||||
console.log(uniqueRoadmaps); // ['JavaScript', 'React', 'Node.js'] |
||||
``` |
||||
|
||||
## Using `for...of` |
||||
|
||||
```js |
||||
const roadmaps = ['JavaScript', 'React', 'Node.js', 'Node.js', 'JavaScript']; |
||||
const uniqueRoadmaps = []; |
||||
for (const roadmap of roadmaps) { |
||||
if (!uniqueRoadmaps.includes(roadmap)) { |
||||
uniqueRoadmaps.push(roadmap); |
||||
} |
||||
} |
||||
console.log(uniqueRoadmaps); // ['JavaScript', 'React', 'Node.js'] |
||||
``` |
@ -0,0 +1,19 @@ |
||||
Map is another data structure in JavaScript which is similar to `Object` but the key can be of any type. It is a collection of elements where each element is stored as a Key, value pair. It is also known as a Hash table or a dictionary. |
||||
|
||||
The `key` can be of any type but the `value` can be of any type. The `key` is unique and immutable, whereas the `value` can be mutable or immutable. |
||||
|
||||
```js |
||||
const roadmap = new Map(); |
||||
roadmap.set('name', 'JavaScript'); |
||||
roadmap.set('type', 'dynamic'); |
||||
roadmap.set('year', 1995); |
||||
|
||||
console.log(roadmap.get('name')); // JavaScript |
||||
|
||||
roadmap.delete('year'); |
||||
console.log(roadmap.has('year')); // false |
||||
console.log(roadmap.size); // 2 |
||||
|
||||
roadmap.clear(); |
||||
console.log(roadmap.size); // 0 |
||||
``` |
@ -0,0 +1,14 @@ |
||||
Set is another data structure in JavaScript which is similar to `Array` but the values are unique. It is a collection of elements where each element is stored as a value without any keys. |
||||
|
||||
```js |
||||
const roadmap = new Set(); |
||||
roadmap.add('JavaScript'); |
||||
roadmap.add('dynamic'); |
||||
roadmap.add(1995); |
||||
|
||||
console.log(roadmap.has('JavaScript')); // true |
||||
|
||||
roadmap.delete(1995); |
||||
console.log(roadmap.has(1995)); // false |
||||
console.log(roadmap.size); // 2 |
||||
``` |
Loading…
Reference in new issue