Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
157
rated 0 times [  162] [ 5]  / answers: 1 / hits: 139656  / 15 Years ago, thu, june 4, 2009, 12:00:00

Linq has a convenient operator method called Take() to return a given number of elements in anything that implements IEnumerable. Is there anything similar in jQuery for working with arrays?


Or, asked differently: how can I truncate an array in Javascript?


More From » arrays

 Answers
108

There is a slice method




let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
arr = arr.slice(0, 4);
console.log(arr);




Will return the first four elements.


Don't forget to assign it back to your variable if you want to discard the other values.


Note: This is just regular javascript, no need for jquery.


[#99375] Wednesday, June 3, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
darennevina

Total Points: 422
Total Questions: 128
Total Answers: 105

Location: Comoros
Member since Tue, Mar 14, 2023
1 Year ago
;