Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript take part of an array

Tags:

javascript

How can I create a new array that contains all elements numbered nth to (n+k)th from an old array?

like image 958
KalEl Avatar asked Nov 11 '09 16:11

KalEl


People also ask

How do you extract an element in JavaScript?

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. We call slice on oldArray with the start and end indexes respectively. The end index is excluded from the returned array.

How do I slice an array in TypeScript?

slice() is an inbuilt TypeScript function which is used to extract a section of an array and returns a new array. Syntax: array. slice( begin [,end] );

How do I extract an array from an array?

The extract() function imports variables into the local symbol table from an array. This function uses array keys as variable names and values as variable values. For each element it will create a variable in the current symbol table. This function returns the number of variables extracted on success.


2 Answers

You want the slice method.

var newArray = oldArray.slice(n, n+k); 
like image 134
Bob Avatar answered Sep 21 '22 15:09

Bob


i think the slice method will do what you want.

arrayObject.slice(start,end) 
like image 34
Eric Avatar answered Sep 21 '22 15:09

Eric