Get a subarray of an array between specified indexes in JavaScript
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:
|
1 2 3 4 5 6 |
let arr = [1, 2, 3, 4, 5]; // get a subarray from index 1 to 3 let subarr = arr.slice(1, 4); console.log(subarr); // [2, 3, 4] |
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:
|
1 2 3 4 5 6 7 |
let arr = [1, 2, 3, 4, 5]; // remove 3 elements from index 1 let subarr = arr.splice(1, 3); console.log(subarr); // [2, 3, 4] console.log(arr); // [1, 5] |
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:
|
1 2 3 4 5 6 |
let arr = [1, 2, 3, 4, 5]; // get a subarray of elements that are greater than 2 let subarr = arr.filter(x => x > 2); console.log(subarr); // [3, 4, 5] |
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:
|
1 2 3 4 5 6 7 8 9 |
let arr = [1, 2, 3, 4, 5]; // another array to a subarray from index 1 to 3 let indexes = [1, 2, 3]; // get a subarray from arr with elements at the indexes specified in the indexes array let subarr = indexes.map(i => arr[i]); console.log(subarr); // [2, 3, 4] |
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.
|
1 2 3 4 5 6 7 8 9 |
let arr = [1, 2, 3, 4, 5]; // to a subarray from index 1 to 3 let indexes = [1, 2, 3]; // get a subarray from arr with elements at the indexes specified in the indexes array let subarr = indexes.reduce((a, b) => {a.push(arr[b]); return a;}, []) console.log(subarr); // [2, 3, 4] |
That’s all about getting a subarray of an array between specified indexes in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)