How do I modify an object value in a cloned array of objects in Javascript

Dave Kalu

I have an array of objects like this:

const arrA = [{a: true}];

And I create a copy of the array like so:

const arrB = Array.from(arrA);

But when I modify the value of the object in arrB, the value in arrA gets modified too.

arrB[0].a = false
// arrB = [{a:false}]
// arrA = [{a:false}] - gets modified also

How do I modify the object values in the cloned array without modifying the original array.

Gibor

Array.from creates what is called a shallow copy. Shallow copy means you have 2 variables- arrA and arrB pointing at the same array in memory, so you change one it changes the other. With objects inside its even more complicated- cuz even if you manage to create different array in memory- it might still point on the same objects as the previous array, leading to again- one change the changes both.

The opposite is deep copy- creating a new array with new objects that hold the same data values, not references. There are a few methods for that, you can read more here, I recommend on JSON.parse/stringify as the easiest one:

const arrA = [{a: true}];
const arrB = JSON.parse(JSON.stringify(arrA));

This will give you what you want :)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I convert array of Objects into one Object in JavaScript?

Java: How do I cast a cloned object to it's original subclass?

How do I convert an array of objects into an object (dynamically) in JavaScript

How do I convert array of objects to object in javascript?

How do I merge an array of objects in Javascript?

In javascript, I have an array of objects. How do I console log the name of the object, rather than the contents?

How can I remove an object from array of objects based on max value in javascript

How do I modify a child object if it exists in array of objects using jq?

How do I turn key value pairs in object into array of objects?

How do I insert array value into object?

How do I create and modify an associative array in JavaScript?

How do I extract an object(array) from an array of objects?

how do I find an object value in a javascript array?

Modify array of objects in JavaScript by setting the key value to one of the object's attributes

how do I find the longest name in an array of objects, then return the entire object to console in javascript?

How do I convert array of Objects to one Object in JavaScript?

How do I filter an array object inside of a array of objects Javascript?

how do you convert javascript object with arrays in to array of objects with corresponding name and value?

How do you convert an array of {"name":"myName","value":"myValue"} objects into an object of "myName":"myValue" pairs in JavaScript?

How Do I Make Multiple Copies of a Javascript Object in and Array and Edit Those Objects Separately

how do I create an array of objects with array in the object from an object with the same key in javascript

How do I check if a particular object exist or not in a JavaScript array of objects?

How do I return an array of objects that only contains a certain object in an array for Javascript?

How do I declare the objects having array of objects in JavaScript?

How to convert an array of nested objects to an object with key, value pairs in JavaScript

How do i filter an array inside of a array of objects value

How do I create an object from array of objects using javascript?

Javascript modify array of objects and concat value

How Do I modify values in objects that is in a lodash grouped object?