Categories
JavaScript Answers

How to Extract Part of an Array with JavaScript?

Spread the love

To extract part of an array with JavaScript, we can use the JavaScript array slice method with the start and end index of the array to extract.

For instance, we can write:

const oldArray = [1, 2, 3, 4, 5]
const newArray = oldArray.slice(1, 3);
console.log(newArray)

We call slice on oldArray with the start and end indexes respectively.

The end index is excluded from the returned array.

So the items at indexes 1 and 2 will be included in the returned array.

Therefore, newArray is [2, 3] .

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published.