2

I want to split my array in half and save the results of that in another array, But without affecting the original.

So if I had [1,3,9,5] I would want to save it in a variable. Then I would create new array and copy the initial one in it. Then I would split that new array in half.

Meaning in the end I would have 2 array like this

initial [1,3,9,5]
halved [1,3]

The problem is that initial one is also splitted and I get 2 array with each one holding half the values.

var initial = [1,3,9,5];

var half = initial;

half = half.splice(0, Math.floor(half.length / 2));

console.log(initial);
console.log(half);

2
  • 2
    splice is destructive, slice is not. I learned it the hard way. Commented Mar 8, 2016 at 14:48
  • Omg. I guess a lot of answers here confused those two. I know I did. Commented Mar 8, 2016 at 14:52

5 Answers 5

1
var half = initial;

copies the reference of initial to half. They’re the same array.

Either copy the values of the array with var half = initial.slice(); or get the half right away with

var initial = [1, 3, 9, 5];
var half = initial.slice(0, Math.floor(initial.length / 2));
Sign up to request clarification or add additional context in comments.

Comments

1

You can do it like this

var initial = [1,3,9,5];

var half = initial.slice(0, Math.floor(initial.length / 2));

console.log(initial); 
console.log(half);  

Comments

1

Check the following:

Option 1 Creating a copy of the initial array

var initial = [1,3,9,5];

var half = [].concat(initial);

half = half.splice(0, Math.floor(half.length / 2));

console.log(initial);
console.log(half);

The [].concat(initial) allows to create a copy of the initial array.

Option 2 Do not modify the initial array, but use slice method

var initial = [1,3,9,5];

var half = initial.slice(0, Math.floor(half.length / 2));

console.log(initial);
console.log(half);

2 Comments

While your option 1 works... it feels like using a sledge-hammer when a regular hammer (slice) is most appropriate. By using concat + spliceyou're essentially mimicking slice but in a less efficient way.
@SebastienDaniel Agreed. The 2 options are showed for a better understanding on the problem. Sure slice() is the correct approach.
1

When splitting an array there are two prototype methods:

  • splice: a destructive version which affects the original array.
  • slice: a pure version, which returns a new array without affecting the array on which it operates.

var half = initial.slice(0,Math.floor(half.length / 2));

Just replace your use of splice with slice and you're good to go.

For more information, I invite you to consult the excellent MDN document for Array.splice and MDN document for Array.slice

Comments

0

When you do var half = initial; you only pass a reference, not copy an array. You need to use other method to copy an array, for example:

var half = initial.slice();

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.