split an array into two based on a index in javascript split an array into two based on a index in javascript arrays arrays

split an array into two based on a index in javascript


Use slice, as such:

var ar = [1,2,3,4,5,6];var p1 = ar.slice(0,4);var p2 = ar.slice(4);


You can use Array@splice to chop all elements after a specified index off the end of the array and return them:

x = ["a", "b", "c", "d", "e", "f", "g"];y = x.splice(3);console.log(x); // ["a", "b", "c"]console.log(y); // ["d", "e", "f", "g"]


use slice:

var bigOne = [0,1,2,3,4,5,6];var splittedOne = bigOne.slice(3 /*your Index*/);