Sometimes, we want to split a long array into smaller arrays with JavaScript.
In this article, we’ll look at how to split a long array into smaller arrays with JavaScript.
How to split a long array into smaller arrays with JavaScript?
To split a long array into smaller arrays with JavaScript, we can use the array splice
method.
For instance, we write
const a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
const b = a.splice(0, 10);
to call splice
with 0 and 10 to remove the first 10 items from array a
and return them in a new array.
Therefore, a
is [11, 12, 13, 14, 15]
and b
is [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
after calling a.splice
.
Conclusion
To split a long array into smaller arrays with JavaScript, we can use the array splice
method.