JavaScript split array | into a chunk, two based on condition

JavaScript split array can be done by using a slice() method. The slice() method returns a shallow copy of a portion of an array into a new array object.

In the slice method, you have to pass the start and ends argument. It returns the value of indexed at the given start argument and ends at, but excluding the given end argument.

Syntax: array.slice(start, end)

Note: split() is a method of the String object, not of the Array object. So you can’t s split array by using the split method.

Examples of JavaScript split array

The array’s first element is at index 0. Let’s examples of how to JS split array into chunks.

Array into many smaller Arrays

The array.slice method can extract a slice from the beginning, middle, or end of an array for whatever purposes you require, without changing the original array.

See below example of Split array into chunks in js.

JavaScript split array into multiple array

How to javascript split array into two-part

Use the slice() method to get the one by one portion of an array into a new array object.

Output:

split an array into two arrays in javascript

Q: How to javascript split array into two based on a condition?

Answer: Here is a code of split an array into two arrays based on odd/even position.

Output: [1, 2, 8, 6]

Do comment if you have any questions and suggestions on this tutorial.

Note: The All JS Examples codes are tested on the Safari browser (Version 12.0.2) and Chrome.
OS: macOS 10.14 Mojave
Code: HTML 5 Version


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.