By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
Notification Show More
Latest News
Passing a JavaScript Value Between HTML Pages
Passing a JavaScript Value Between HTML Pages
JavaScript
Compare Objects in an Array
JavaScript Problem: Compare Objects in an Array
JavaScript
Switching Name Order Using Capturing Groups in Regular Expressions
Switching Name Order Using Capturing Groups in Regular Expressions
JavaScript
Shuffling an Array
JavaScript Problem: How to Perform Shuffling an Array
JavaScript
Create a Non-duplicated Collection
JavaScript Problem: Using Set to Create a Non-duplicated Collection
JavaScript
Aa
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > JavaScript > JavaScript Problem: Using Set to Create a Non-duplicated Collection
JavaScript

JavaScript Problem: Using Set to Create a Non-duplicated Collection

Admin
Last updated: 2023/01/26 at 6:12 AM
Admin
Share
3 Min Read
Create a Non-duplicated Collection

In this article, we are going to see how we can create a Non-duplicated Collection using set. So in JavaScript we usually work with array, right? And we know that array can consist of duplicate value. Creating an array of values without duplicating a value takes a bit of work. You need to check each new value. ES6 provides an easier way of doing that by creating a set.

Contents
Create a Non-duplicated Collection: Using SetCreate a Non-duplicated Collection: Iterate Through SetYou may also like:

Create a Non-duplicated Collection: Using Set

Set is similar to an array which can be used to store data, but set becomes different when it comes to duplicate values. An array can have duplicate values, on which we can perform some logic to make it duplicate free. But does not take value which are already present in the set. Let’s take an example to understand:

Create a Non-duplicated Collection

As we can see, in the above example we have made a blank set using Set(), and we added some values using add() method. In this set, we have added 3 values, and if we try to add the same value which is already presented in the set, that value won’t get added in the set.

Now, what if we already have an array with duplicate values? Let’s see how we can make a set without duplicate values.

Create a Non-duplicated Collection

As you can see, we have made an array with duplicate values, and now we can pass this array in Set() while creating a set. This will simply make a set which have unique values. You can also convert this set back to array, you can pass this set to array with spread operator.

Create a Non-duplicated Collection

Create a Non-duplicated Collection: Iterate Through Set

Okay, we know that we can access every value using some simple logic and loop. To access every value in the set, we can use the forEach() method. forEach() method helps us to iterate through the set, it takes a callback function to do some operation related to the set’s values.

In the below example, we have created a set with an array of duplicate values and defined a sum variable. Now we applied the forEach() method on the set, in which we added a function in which we are just adding all values greater than two.

let col = new Set([1,2,3,4,5,2,3,5,2,6]),
    sum = 0;
        
col.forEach(function(val){
    if(val>2){
       sum += val;
    }
})
console.log(col);
console.log(sum);
Create a Non-duplicated Collection

You may also like:

  • How to Define a Class With Properties and Methods in JavaScript?
  • How to Implement Class Inheritance in JavaScript?
  • How to Find Duplicate Elements in a Given Array?

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

TAGGED: Create a Non-duplicated Collection, javascript
Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Posted by Admin
Follow:
Rocoderes is a blog you can learn HTML, CSS, JavaScript, React Js and Python along with creative coding stuff and free source code files.
Previous Article Formatting Time Using The Date Object and String Padding Formatting Time Using The Date Object and String Padding
Next Article Shuffling an Array JavaScript Problem: How to Perform Shuffling an Array
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

Passing a JavaScript Value Between HTML Pages

Passing a JavaScript Value Between HTML Pages

February 3, 2023
Compare Objects in an Array

JavaScript Problem: Compare Objects in an Array

January 30, 2023
Switching Name Order Using Capturing Groups in Regular Expressions

Switching Name Order Using Capturing Groups in Regular Expressions

January 29, 2023
Shuffling an Array

JavaScript Problem: How to Perform Shuffling an Array

January 27, 2023
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?