Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convenience method to get sub-indices of array in javascript

Tags:

javascript

Is there a convenient way to get say the first n number of indices from this array:

var alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];

and store it in another array, say alphabet2 = ['a','b','c']; without having to brute force with a loop? Thanks

like image 705
Apollo Avatar asked Jan 28 '26 18:01

Apollo


1 Answers

Use the slice method:

> var alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
> alphabet.slice(0,3)
["a", "b", "c"]
like image 151
Dave Newton Avatar answered Jan 30 '26 06:01

Dave Newton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!