Create a duplicate of array A in JavaScript with the keys transferred to array B, then make modifications to the elements in

Currently, I am delving into JavaScript in regards to array assignment and cloning. A puzzling issue arises when attempting to clone the elements of array A into array B using the spread operator "...". Surprisingly, when modifying the elements in array B, the elements in array A also seem to be affected.

For instance:

A = [{id:1, value:3},{id:2, value:1}];
B = [...A];
B[0].value = 4;
console.log(A[0].value);//the output changes to 4, not 3

I've noticed that this issue does not occur when cloning a regular array:

A = [3, 1];
B = [...A];
B[0] = 4;
console.log(A[0]);//the output is still 3

It is known that one purpose of cloning in JavaScript is to prevent two variables from referencing the same memory location. However, why does altering an element in array B reflect on array A as well? If there are any misconceptions on my part, I would appreciate your guidance in clearing them up. Alternatively, if this problem has been addressed and resolved in past discussions, kindly share the relevant link.

Answer №1

To better understand how A works, it's helpful to not view it as directly holding the objects at specific indexes, but rather as holding references to those objects:

const A = [*refA*, *refB*];

In this context, refA and refB point to your {id:1, value:3} and {id:2, value:1} objects respectively.

When you create a new array using [...A], each reference is duplicated in the new array but still points to the same underlying objects in memory:

B = [...A]; // `B` is [*refA*, *refB*]

So if you update B[0].value = 4, you're actually modifying the object referenced by B[0] (i.e., *refA*) and changing its value to 4. Since both A and

B</code share the same object references, changes will be reflected in both arrays.</p>
<p>To prevent this shared reference behavior, you can use <code>.map()
to create a new array with distinct object references:

const B = A.map(obj => ({...obj}));

In this new array creation process, we are generating unique objects by spreading the properties of each object into a fresh object. By doing so, updating B[0].value = 4 won't affect the objects held in A.

Answer №2

When it comes to cloning objects, the Spread Operator and Object.assign() both only perform shallow cloning up to one level. For deeper cloning, utilizing a popular library such as <code>loadash
is recommended, which offers a deepClone() function.

Another common technique used for JSON data, excluding functions, is:

let deepClone = JSON.parse(JSON.stringify(nodesArray));

You can give this method a try as well :)

Answer №3

The explanation behind this phenomena is rooted in the fact that the initial example contains an array of objects, all of which are reference variables.

When you create a new instance of the array, you are not actually generating new objects. Rather, you are passing references to the same objects that were utilized in the original array.

Answer №4

When utilizing Spread, a new reference is created for the array. However, this method only clones one level deep. Now, array A and array B are pointing to distinct references.

Yet, when it comes to objects (1st case), the elements within the array still reference the same object. This distinction is not present with primitives (2nd case). Thus, altering B[0] impacts the first element of array B, which is a primitive value that gets replaced.

Answer №5

Why do the elements of Array A also change when I modify B's elements?

This happens because objects typed values are copied by reference, while primitive typed values are copied by value. This means that in your second example, a deep copy is performed (values were copied), while in the first example a shallow copy is done (references were copied).

This is the concept of shallow copy and deep copy.

Now, let's discuss the spread operator: It performs a deep copy if the data is not nested. For nested data, it deeply copies the first level data and makes shallow copies of the nested data.

Therefore, if you need to deep copy your non-primitive data type values, you can use the structured clone algorithm.

Demo :

const A = [{id:1, value:3},{id:2, value:1}];
const B = structuredClone(A);
B[0].value = 4;
console.log(A[0].value); // 3
console.log(B[0].value); // 4

Answer №6

To successfully clone an Array, you must perform a deep copy instead of a shallow copy using the ... operator.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the process by which Oracle HRMS retrieves the complete description of a job posting?

With my limited understanding of JavaScript, I noticed that the HRMS refreshes the page and displays the job details under the same URL as the job list page. I expected to find job detail information in the network requests, but upon inspecting them, I dis ...

Object3D.frustumCulled property in ThreeJS helps determine if an object

In ThreeJS, Object3D objects have a built-in function to determine if they are within the camera's view - Object3D.frustumCulled. Is there a way to access and retrieve the result of the "Object3D.frustumCulled" check? ...

There was an error during product validation that occurred in the userId field. In the Node.js application, it is required to

I am in the process of developing a small shop application using node.js, express, and mongoose. However, I have encountered an issue when attempting to send data to the MongoDB database via mongoose. Here is the product class I have created: const mongoo ...

Tips for assigning a numerical value to an array and showcasing it in a listview

I have a dilemma involving two activities. The first activity displays a list of items using a ListView. In the second activity, I also want to display a list of items, but the content will vary based on the position of the element selected in the first ac ...

Using JavaScript to enhance event handling for a `select` element in a progressive manner

I have an advanced <select> element in my HTML. It is structured like this: <ul> <li></li> <li></li> ... </ul> In the current setup, a click event handler is attached to each individual li element. If the ...

Arranging objects in an array based on a separate array of strings

Here is an array of objects that I need to rearrange: var items = [ { key: 'address', value: '1234 Boxwood Lane' }, { key: 'nameAndTitle', value: 'Jane Doe, Manager' }, { key: 'contactEmail', value: ...

React JS server conditional response malfunctioning

I've been facing issues with receiving a conditional response from an Express server for a React application. Check out the server-side code below: app.get('/api/checklogin', (req, res) => { var val = req.session.user ? false : tru ...

Dynamically generating an array and seamlessly inserting it into a MySQL database using PHP

I am looking to generate an array that will be used in an insert query, similar to the following: $data=array( 'db_field1'=>$value, 'db_field2'=>$value, 'db_field3'=>$value, and so forth ); $this->db->inser ...

Issue with returning value from promise object in Next.js

Hello! I am fairly new to JS and React, so I would appreciate your patience as I try to navigate through this. It has been quite a journey so far. In my component, I am fetching JSON data from a URL and attempting to extract a specific value from it to d ...

Navigate back to the previous route within the Vue router hierarchy

In my Vue application, I have a Settings page with child routes such as settings/user, settings/addUser, etc. I am looking to implement a back button that when pressed, takes the user back to the specific page they visited within the Settings section. Usin ...

How does a JS event impact the state transition in a backend state machine built on Rails?

I am currently developing a new project where the backend involves Users who have multiple "Courses" each containing multiple "Steps". My goal is to create a JavaScript function that validates the user's answer. For example, if the user inputs "6", w ...

Is there a way to dynamically update the target of a form using JavaScript?

My goal is to change the target of a form in order to open a new window. However, all redirections of this form use the same window except for one specific button. This button calls a custom JavaScript function that needs to change the default "_self" targ ...

What is the process for implementing a component in antdesign when using vue-cli and vue 3?

I followed the instructions provided in the documentation here. These are the steps I took: vue create example-app cd example-app I selected the vue 3 preset. Then, I made changes to the script in main.js import Vue from 'vue'; import Button f ...

Vue Framework 7 incorporates a validation feature that ensures successful outcomes

In my current project using Framework7 Vue with version 4.4.3, I am facing a challenge in validating a form upon submission. I came across this helpful code snippet: $$('.save').on('click', function(e){ e.preventDefault(); if ...

Is there a problem with undefined values for dynamic inputs?

When I execute console.log($('#mat_code_' + counter - 1).val());, I receive an undefined value even though the inputs are empty. This is the code snippet : $('#m_table').on('click', '.check', function() { ...

Transferring data from a React file to a plain HTML page

What is the best way to transfer information from a React app to a static HTML file? For instance, if I collect data through a form in a React app.js file, how can I then send that data to a basic HTML file? ...

Prevent TypeScript from generalizing string literals as types

I have a constant Object called STUDY_TAGS with specific properties const STUDY_TAGS ={ InstanceAvailability: { tag: "00080056", type: "optional", vr: "string" }, ModalitiesinStudy: { tag: "00080061", type: " ...

Managing date validation for a three-part field using AngularJS

I am currently working on validating a three-part date field using AngularJS. I have managed to create a custom validation function, but I am struggling with determining how the fields should update each other's status. How can I ensure that all thre ...

Building an anchor tag that employs the HTTP DELETE method within an Express.js app

Recently, I delved into using express.js with handlebars.js as my template engine. One task I wanted to tackle was creating a delete link that followed RESTful principles and used the HTTP DELETE verb instead of GET. After some trial and error, I discover ...

Using AJAX and JQuery to automatically refresh a webpage on a scheduled interval

I am attempting to automatically refresh a page every 5 seconds with updated data from an SQL Server. Below is my code snippet: @model Test.Data.Domain.ManufacturingCdMachine @{ ViewBag.Title = "Rimage Details"; ViewBag.JobId = Model.CurrentManufa ...