Warning: session_start(): open(/tmp/sess_5fcaed6f32587671b72b0c2d71639457, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How To Create A Shallow Copy Of A Set In JavaScript - LearnShareIT

How To Create A Shallow Copy Of A Set In JavaScript

How to create a shallow Copy of a Set in JavaScript

Suppose you need help with how to create a shallow copy of a set in JavaScript. Don’t worry too much because we have already tested effectively using the Set() constructor to create a shallow copy of SetLet’s learn with us to know how it works.

What is the shallow copy?

To know how to create a shallow copy of a set in JavaScript, the first thing you must know is what a shallow copy is.

Understand simply, a shallow copy means that the new variable or its components are still related to the original variable after copying. For shallow copies, it’s essential to understand that selectively changing the value of a shared property of an existing element in an object is different from assigning a completely new value to an existing element.

Let us take an example to understand it better about the shallow copy.

Example:

In the example below, even though a is defined as const, we can still change the value. Do you know why? Back in the day, we actually made a shallow copy in the example above. This will often cause errors if we use it inappropriately. Instead of changing the value of the new variable, we also change the value of the original variable.

const player = {
	name: "Cristiano Ronaldo",
	age: 37,
	nationality: "Portugal",
	salary: "26,8 million GBP"
};

console.log("Player name => ", player.name);
var newPlayer = player; // Shallow copy
console.log("New Player name => ", newPlayer.name);

Output

Player name => Cristiano Ronaldo
New Player name => Cristiano Ronaldo

Create a shallow copy of a Set in JavaScript

Using the Set() constructor

To create a shallow copy of a set in JavaScript, we just use the constructor to clone the Set. Just only like this:

var clonedSet = new Set(originalSet);

The Set constructor lets you create Set objects that store unique values of any type, whether primitive values or object references.

To compare two sets after create a shallow copy, we already have the article to show some ways to compare two Sets here.

Syntax:

  • New Set()
  • New Set(iter)

Parameter:

  • iter: all elements of iter will be added to the new element if this parameter is non-empty.

Return value: A new Set object.

Code example:

Let’s see the code example below.

const mySet = new Set(["Croatia", "Argentina", "Morocco", "France"]);
console.log("My Set =>", mySet);

// Create a shallow copy of mySet
const newSet = new Set(mySet);
console.log("New Set =>", newSet);

// Two Set is the same value but not equals
console.log("Comparing Set and newSet: ", mySet === newSet); // false

Output

My Set => Set(4) {size: 4, Croatia, Argentina, Morocco, France}
New Set => Set(4) {size: 4, Croatia, Argentina, Morocco, France}
Comparing Set and newSet:  false

Browser Support

The Set constructor is an ECMAScript1 (ES1) feature.

This feature is supported in all browsers, and you can use it for all browsers. Very common function in JavaScript.

Summary

Hopefully, through this article, you can easily understand how to create a shallow copy of a Set in JavaScript. Using the Set constructor is the way we have shown in this article, but how about you? Leave your comment here if you have any questions about this article. Thank you for your reading!

Maybe you are interested:

Leave a Reply

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