Javascript – Split array into two arrays

javascriptjquery

var arr = ['a', 'b', 'c', 'd', 'e', 'f'];
var point = 'c';

How can I split the "arr" into two arrays based on the "point" variable, like:

['a', 'b']

and

['d', 'e', 'f']

Best Solution

var arr2 = ['a', 'b', 'c', 'd', 'e', 'f'];
arr = arr2.splice(0, arr2.indexOf('c'));

To remove 'c' from arr2:

arr2.splice(0,1);

arr contains the first two elements and arr2 contains the last three.

This makes some assumptions (like arr2 will always contain the 'point' at first assignment), so add some correctness checking for border cases as necessary.