Split Array

S

Sunny

Hi,

Does anyone know, How to split an array?
Like If I have 10 Numbers in an Array.
4,5,6,1,2,3,4,8,5,9
And I want to break that array from 3 & I want another array to have
same values but in this order.
3,4,8,5,9,4,5,6,1,2
Like I am breaking from 3 and then putting the second part first &
then the first & joining them.
Does anyone know, How to do that?
 
S

sasuke

Hi,

Does anyone know, How to split an array?
Like If I have 10 Numbers in an Array.
4,5,6,1,2,3,4,8,5,9
And I want to break that array from 3 & I want another array to have
same values but in this order.
3,4,8,5,9,4,5,6,1,2
Like I am breaking from 3 and then putting the second part first &
then the first & joining them.
Does anyone know, How to do that?

<URL: http://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Predefined_Core_Objects/Array_Object>
should get you going in the right direction.

/sasuke
 
E

Evertjan.

Conrad Lender wrote on 10 okt 2008 in comp.lang.javascript:
Does anyone know, How to split an array?
Like If I have 10 Numbers in an Array.
4,5,6,1,2,3,4,8,5,9
And I want to break that array from 3 & I want another array to have
same values but in this order.
3,4,8,5,9,4,5,6,1,2

Short but not very efficient:

var arr = [4,5,6,1,2,3,4,8,5,9];
while (arr[0] != 3) {
arr.push(arr.shift()); // or: arr.unshift(arr.pop());
}

<script type='text/javascript'>

var oldArray = [4,5,6,1,2,3,4,8,5,9];

var newArray = oldArray.slice(5,10);
newArray = newArray.concat(oldArray.slice(0,5))

alert(newArray)

</script>

or

<script type='text/javascript'>

var oldArray = [4,5,6,1,2,3,4,8,5,9];

var newArray = [];
newArray = newArray.concat(oldArray.slice(5,10),oldArray.slice(0,5))

alert(newArray)

</script>
 
D

dhtml

Evertjan. said:
Conrad Lender wrote on 10 okt 2008 in comp.lang.javascript:
Does anyone know, How to split an array?
Like If I have 10 Numbers in an Array.
4,5,6,1,2,3,4,8,5,9
And I want to break that array from 3 & I want another array to have
same values but in this order.
3,4,8,5,9,4,5,6,1,2
Short but not very efficient:

var arr = [4,5,6,1,2,3,4,8,5,9];
while (arr[0] != 3) {
arr.push(arr.shift()); // or: arr.unshift(arr.pop());
}

<script type='text/javascript'>

var oldArray = [4,5,6,1,2,3,4,8,5,9];

var newArray = oldArray.slice(5,10);
newArray = newArray.concat(oldArray.slice(0,5))

alert(newArray)

</script>

or

<script type='text/javascript'>

var oldArray = [4,5,6,1,2,3,4,8,5,9];

var newArray = [];
newArray = newArray.concat(oldArray.slice(5,10),oldArray.slice(0,5))

alert(newArray)

</script>

Since slice returns an array, it could be used without having to create
a new one.

var items = [4,5,6,1,2,3,4,8,5,9];
var newArray = items.slice(5,10).concat(items.slice(0,5));

alert(items[0] === 4); // doesn't change items.


Or push.apply:
var items = [4,5,6,1,2,3,4,8,5,9];
var start = items.slice(5);

start.push.apply(start, items.slice(0,5));

Garrett
 
R

RobG

JFTR, all of these would require an additional loop to find the index of
the element with the value 3.

If that was a requirement, yes. *But* the OP didn't say how the index
used to split the array was chosen, only that for the example it was
from the element with value 3 (i.e. index 5).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,138
Messages
2,570,802
Members
47,348
Latest member
nethues

Latest Threads

Top