2

I am trying to make a copy of a javascript array and modify the 'copy' without modifying the original variable too, can anyone explain what I am doing wrong..

e.g

var array1 = [2, 5];
var array2 = '';

array2 = array1;
array2.pop();
console.log(array1);
console.log(array2);

// both output the same, however I want it to show array1 with 1 single item

I am trying to make it so array2 will only contain the one item in the array & array1 will contain two items in the array. Any ideas what I'm doing wrong?

5 Answers 5

2

In order to create a copy of an array rather than assign the reference value you can use the .slice method.

array2 = array1.slice(0);

Slice returns a (shallow) copy of the given array from the given index to the (optional end index)

Sign up to request clarification or add additional context in comments.

3 Comments

array2 = array1.slice(1); - this seems to work for me using 1 rather than 0, is that still correct (sorry for the noob questions) :)
@Zabs You don't have to apologize for asking questions :) That's what this site is for. if you use .slice(1) it returns a slice of array1 not containing the element at position 0.
Thanks dude! +1 for the explanation :)
1

Use slice() to copy the array.

    var array1 = [2, 5];
    var array2 = '';

    array2 = array1.slice(0);
    array2.pop();
    console.log(array1);
    console.log(array2);

slice does not alter the original array, but returns a new "one level deep" copy that contains copies of the elements sliced from the original array.

Documentation

Comments

0

It is as simple as using the slice function:

var array1 = [2, 5];
var array2 = '';

array2 = array1.slice();
array2.pop();
console.log(array1);
console.log(array2);

Here you are telling javascript to get a copy of the elements in array1 and assign the result to array2. By not setting the start and ending position it will slice the whole array.

Comments

0

When you writing this

array2 = array1 //It creates reference on array1 object
array2.pop()  // removes 5 from both end

So you have to clone one array to another by slice() method---

array2 = array1.slice(0) // cloning array1 's  element  to array2
array2.pop() //  removes 5 from only array2 not from array1

Now it works fine

4 Comments

array1 is a reference
No. array2 and array1 are both references (pointers to objects).
if i write array2=array1 that means,array2-->array1-->[2,5],so whenever we pop item from array2 that means it popped item from array1,thats why 5 is popped from both end
No. array2 --> [2,5] <-- array1. Like I told you, array1 and array2 are pointers to objects. Every value in JavaScript must be either a primitive or a pointer to an object. You fundamentally do not understand how values in JavaScript work.
0

Alter the line array2 = array1; to:

array2 = array1.slice(0);

Slice method returns a (shallow) copy of the given array from the given index to the (optional end index)

I hope this helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.