Assigning arrays to each other

Tell us what’s happening:
The tests fail when executing the following code and it made wonder if it is possible to
assign an array to another array in javascript ?

Your code so far


// Setup
var collection = {
2548: {
  album: "Slippery When Wet",
  artist: "Bon Jovi",
  tracks: [
    "Let It Rock",
    "You Give Love a Bad Name"
  ]
},
2468: {
  album: "1999",
  artist: "Prince",
  tracks: [
    "1999",
    "Little Red Corvette"
  ]
},
1245: {
  artist: "Robert Palmer",
  tracks: [ ]
},
5439: {
  album: "ABBA Gold"
}
};

// Only change code below this line
function updateRecords(id, prop, value) {

if(!value)
{
  delete collection[id][prop];
}
else if(prop == "tracks")
{
  if(collection[id].hasOwnProperty("tracks"))
  {
    collection[id][tracks].push(value)
  }
  else
  {
    var tempArr = [];
    tempArr.push(value)
    collection[id][tracks] = tempArr;
  }
}
else
{
  collection[id][prop] = value;
}

return collection;
}

updateRecords(5439, "artist", "ABBA");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36.

Challenge: Record Collection

It depends on what you mean. Remember that in JS (and many languages) the array is a reference type. So, the variable by which you call the array is not the array it self but is a reference (or memory address) for the section of memory that contains the array.

So, when I go:

const arr1 = [1, 2, 3];
const arr2 = arr1;

There is only one array. But there are two variables that contain references to the same array. Whatever you do to one changes the other because they are exactly and completely the same array. This often causes confusion with new programmers. The same principle will apply to all reference types (anything that isn’t a primitive).

So, can you copy an array, in the sense of getting a separate and unique copy? We often call this cloning. Yes, you can write a recursive algorithm that creates a new and unique clone. There are also packages like lodash, which has the method cloneDeep. Another (slightly ugly, imho) to do it would be to JSON.stringify it and then JSON.parse it - assuming it is stringifiable.

There is a discussion here of different options. There is no discussion of the recursive option, but I’m sure if you search “javascript recursive array clone” you’ll find some discussions.

ah Okay, so in Javascript as well the deep copy is not automatic and if I assign a global array to a local array, the global array will be pointing to nothing after the scope of the local array is terminated. Did I understand correctly ?

Thank you, it is clear now

ah Okay, so in Javascript as well the deep copy is not automatic

That is correct, and is correct in every language I’ve learned. Of course, I haven’t leaned them all.

if I assign a global array to a local array, the global array will be pointing to nothing after the scope of the local array is terminated.

You don’t assign arrays you assign references to arrays. If two array references point to the same array and then one goes out of scope. I think JS is smart enough to know that that array is still in use so it won’t be garbage collected.

My understanding of JS memory “garbage collection” is that it doesn’t care what variable originally pointed to that memory, all it cares is if any variable still points to it - if nothing points to it, it is unused and so that memory location can be recycled.

Okay, it is clearer now. I always assumed that javascript abstracts references and these kind of stuff but I was wrong.

Thank you Kevin.

Am not getting your question right but if you want to assign an array to another ya its possible you can do it like this

const arr1= [1, 2, 3];
const arr2= arr1;

array2[1] = 10;
console.log(arr2);
console.log(arr1);

as we know that array are reference type making any change to any of above will bring change in another

but if you want to assign array to another as a copy and wanna create independent addresses for both on memory so any change to any of them will not effect the other one you can do it like this which is called concatenation in JS

const array1 = [1, 2, 3];
const array2 = […array1];

array2[1] = 10;
console.log(array2);
console.log(array1);

Hope it was helpful

Keep in mind that this will only do a shallow copy. It will work in this case because everything in the array are primitives. This will copy everything on that first level as is, including references. So, if those were arrays or objects or functions, then this wouldn’t work, at least not in a deep clone sense. You run into the same “opps I copied the reference instead of the object/array” problem, just on the next level up.

Maybe you only have an array of primitives. Or maybe you want to copy the references. But if you want a deep clone, it’s probably going to be something recursive.

1 Like