5

I want to slice elements of one array into a new left and right array. I am stuck on how to start this.

3
  • 6
    Three W3 Schools answers within a minute. What is wrong with this world? :) Commented Apr 2, 2011 at 21:52
  • let's not bring name of this site here :) Commented Apr 2, 2011 at 21:54
  • 5
    But let's bring this name here: W3 Fools Commented Apr 2, 2011 at 22:37

6 Answers 6

4

You'll want to start with the "slice" method. Note that it returns a NEW array without changing the old array, so you'll want to go back and mutate the old array as well.

For instance:

var a = [1,2,3,4,5],
    b = a.slice(3);

a.length = 3;
// a is now [1,2,3]
// b is now [4,5]
Sign up to request clarification or add additional context in comments.

4 Comments

you note that the original array won't change, but then in your example you show it changed???
Yep. It's three lines, try it yourself. =)
Line three (a.length = 3) changes the original array.
ahh, you're manually setting the length, missed that, thought you were saying that a.length was 3.
2

Given this array:

var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

If you want to split the array so that the first 4 items go to the left array and the rest to the right array, then do this:

var leftArr = arr.slice(0, 4);

and

var rightArr = arr.slice(4);

You can make a function that returns those two arrays based on the split-position:

function splitArr(arr, i) {
    return [ arr.slice(0, i), arr.slice(i) ];
}

Comments

2

umm..... have you looked at .slice() ?

new_array = your_array.slice( start_index , end_index );

This creates a completely new array, not a reference.

Comments

1

I'd use splice.

var a=[1,2,3,4,5,6];

var b=a.splice(3,a.length);


now a= [1,2,3]
and b=[4,5,6]

3 Comments

Just var b = a.splice(3); is fine too. The second argument is not needed.
@Šime Vidas- that's what I thought, until I tried it in IE. IE (before version 9) does nothing to the array and returns an empty array, without the second argument.
You're right. This is odd. Microsoft's own reference states that the second argument is optional. Btw, I also checked the old ECMAScript spec (ES3 from 1999.) - same thing. This feature is old, I have no idea why Microsoft didn't implement it until IE9.
0

From http://www.w3schools.com/jsref/jsref_slice_array.asp :

Syntax :

array.slice(start, end)

Example :

<script type="text/javascript">
  var fruits = ["Banana", "Orange", "Apple", "Mango"];
  document.write(fruits.slice(0,1) + "<br />");
  document.write(fruits.slice(1) + "<br />");
  document.write(fruits.slice(-2) + "<br />");
  document.write(fruits);
</script>

Output :

Banana
Orange,Apple,Mango
Apple,Mango
Banana,Orange,Apple,Mango

Comments

0
<html>
<body>

<script type="text/javascript">

var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.write(fruits.slice(0,1) + "<br />"); //Banana
document.write(fruits.slice(1) + "<br />"); //Orange,Apple,Mango
document.write(fruits.slice(-2) + "<br />"); //Apple,Mango
document.write(fruits); //Banana,Orange,Apple,Mango

</script>

</body>
</html>

(Reference from w3schools): http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_slice_array

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.