This post will discuss how to get a subarray of an array between specified indexes in JavaScript.

To get a subarray of an array in JavaScript between specified indexes, we can use one of the following functions:

1. Using slice() function

The slice() function returns a shallow copy of a portion of an array into a new array object selected from start to end (end not included) where start and end represent the index of items in that array. The original array will not be modified. Here’s an example:

Download  Run Code

 
In this example, arr.slice(1, 4) returns a new array with the elements at indexes 1 and 3 (inclusive). This function is simple and elegant, but it may not be very efficient for large arrays, as it has a quadratic time complexity. It also requires ES6 support or a polyfill for older browsers.

2. Using splice() function

Another approach is to use the splice() function. The splice() function changes the contents of an array by removing or replacing existing elements and/or adding new elements in place. It returns an array containing the deleted elements. Here’s an example:

Download  Run Code

 
In this example, arr.splice(1, 3) removes three elements starting from index 1 and returns them as a new array. This function modifies the original array and returns a subarray of the removed elements. It is useful when we want to delete some elements from the original array and create a subarray from them. However, it may not be suitable when we want to preserve the original array. This function is compatible with older browsers.

3. Using filter() function

The filter() function creates a new array with all elements that pass a test implemented by a provided function. We can use this function to filter out the elements that match the specified indexes. It also does not change the original array. Here’s an example:

Download  Run Code

 
This function is more functional and expressive than loops, but it may have some performance overhead and compatibility issues. It is useful when we want to create a subarray based on a specific condition.

4. Using map() function

The map() function creates a new array populated with the results of calling a provided function on every element in the calling array. We can use this function to map the specified indexes to the corresponding elements in the original array. For example, if we have an array [1, 2, 3, 4, 5] and we want to get a subarray with elements at indexes 1, 2, and 3, we can use below:

Download  Run Code

5. Using reduce() function

The reduce() function executes a provided reducer function on each element of the array, resulting in a single output value. We can use a function that pushes the element at the current index to an accumulator array if the index is in the desired range or in an array of indexes. For example, we can use indexes.reduce((a, b) => {a.push(arr[b]); return a;}, []); to get a subarray from arr with elements at the indexes specified in the indexes array.

Download  Run Code

That’s all about getting a subarray of an array between specified indexes in JavaScript.