Can you explain the contrast between deep and shallow cloning techniques?

When I employ splice to duplicate an array, I receive a shallow copy. However, there seems to be something missing since I end up with multilevel arrays. It appears that the issue lies elsewhere, possibly not related to the array's depth. Can someone clarify this for me?

Answer №1

When creating a shallow copy, only the references to objects within arrays (or object properties) are duplicated, not the actual objects themselves.

var x = [{name: "Alice"}];
var y = x.slice(0);
y[0].name = "Eve";
alert(x[0].name); // "Eve"

On the other hand, a "deep" copy ensures that the duplicated structure contains entirely new copies of all objects referenced in the original data. However, performing a deep copy can present challenges depending on the complexity of the objects being copied.

Answer №2

To grasp the concept of deep and shallow cloning, consider the following analogy:

  1. Shallow cloning does not serialize the reference object that is present.

  2. On the other hand, deep cloning serializes both the object reference and the current object. This process involves considerations such as pools, object references, and other factors.

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

Encountered an error in NodeJs with mongoDB: TypeError stating that property 'fullname' is not defined

I am having issues storing user-entered data from an HTML form into MongoDB. The error message I receive is "TypeError: cannot read property 'fullname' of undefined" TypeError: Cannot read property 'fullname' of undefined at C:&bso ...

Error encountered: Difficulty rendering Vue 3 components within Google Apps Script

Currently delving into Vue and Vue 3 while coding an application on Google Apps Script. Following tutorials from Vue Mastery and stumbled upon a remarkable example by @brucemcpherson of a Vue 2 app functioning on GAS, which proved to be too challenging in ...

Passing data as a parameter to a page in React using history push

My Ionic React application includes functionality where the history.replace method is used to redirect users from the settings page to the login screen upon clicking a logout button. I am looking for a way to include a loggedOut flag in the redirection pro ...

What is the process for generating an array of objects using JavaScript?

I am struggling to create an array of objects using JavaScript and facing errors with new lines added where I need to split the messages and collect row numbers. The row numbers should be comma-separated if it is a repetitive error message. I found a solu ...

Is there a way to align the Material UI mobile menu to the left edge of the screen?

Need help with this UI issue: Currently working on designing a mobile web app using React and Material UI. I'm struggling to get the sidebar menu to start from the left edge of the screen, as it seems to have an unwanted margin preventing it. Any sug ...

How can I implement Javascript for tracking webshop activity and affiliate links across multiple websites?

I operate a small front end for a webshop where I receive 5% of the sale amount for every customer who makes a purchase after being referred from my website. However, I am struggling to track these customers and need help in setting up a system to monitor ...

What is the best way to use toggleClass on a specific element that has been extended

I have been experimenting with this code snippet for a while. The idea is that when I hover my mouse over the black box, a red box should appear. However, it doesn't seem to be working as expected. Could someone please review this script and let me k ...

differentiating between user click and jquery's .click() method

There are <a href=".. tags on a page with an inline onclick attribute. If a user clicks one without being logged in, they will be prompted to log in. After logging in and the page refreshes, jQuery will trigger .click() on the <a> element to redir ...

Sort your Laravel pagination table effortlessly with just one click

Here is my current setup: <table class="table table-striped"> <tr> <th> Item Image </th> <th> Item Number </th> <th> Item Name </th> <th> Description </th> ...

Modifying the design of a website in real-time using the EXPRESS.js and NODE.js frameworks

I successfully set up a simple website using node.js and express.js by following this helpful tutorial. My express implementation is structured like this with a jade file for the web interface. // app.js var express = require('express'), r ...

Unable to execute multiple instances of Selenium PhantomJS concurrently

I have encountered an issue while using Selenium's node.js API to run PhantomJS instances against a series of web pages. The code that I have written to perform actions on the pages is functioning correctly, but it appears that only one instance of Se ...

Unexpected behavior with the array of objects

I am looking to structure a JSON in the following format: How can I insert multiple objects into a single array? { Data: [ {"dataType":"com.google.weight", "startDate":"2021-04-1308:00", "endDate&qu ...

Expanding/Combining entities

I'm facing an issue while trying to Extend/Push/Merge an object using AngularJS. The problem arises when I attempt to extend the object, as it adds a new object with an Index of 0 and subsequent additions also receive the same index of 0. //Original ...

Filter a Two-Dimensional Array Based on Elements from a Different Array

With two sets of numeric elements stored as 2D arrays, one representing a full list and the other a partial list, I am seeking a function that can return the full list minus the partial list. The partialListArr may contain duplicates, while the fullListA ...

What is the best way to display a modal box using NodeJs on the server side?

I recently created a contact page on my website where users can fill out a form. Upon submitting the form, I capture the information using a post method in my server.js file. If everything is correct, I want to display a modal box on the contact page to co ...

Error: Attempting to access 'title' property of undefined object leads to Uncaught TypeError

I am attempting to extract content from a Google Blogger Feed that can be found at this link. I am using JavaScript code from here. When inspecting the elements, I encountered a specific red warning message: Uncaught TypeError: Cannot read property ' ...

Exploring the functionalities of can-deactivate without incorporating routing

I'm facing an issue with a parent component and multiple child components. The parent component contains a form, while each child component also has its own form. Unfortunately, all child components share the same route as the parent component and hav ...

angular-recaptcha: Adjusting language based on the website's language update

My website offers three different languages that users can switch between. The language switch functionality is implemented on the client side using JavaScript (AngularJS). I have integrated reCAPTCHA 2 into my website and now I need to update the languag ...

Empty array returned when using fetch in a for loop

Currently, I am developing a server route to execute API calls. I have encountered the need to make two separate fetch requests as I require additional information that is not available in the first fetch. The issue lies in declaring a variable outside o ...

What is the reason file inputs do not trigger 'input' events, while 'change' events do fire?

Trying out a basic input: <input type="file"/> Noticing that the input event doesn't trigger when a new file is selected: $('input').on('input', function(event){ console.log('input value changed', event.targe ...