Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
64
rated 0 times [  68] [ 4]  / answers: 1 / hits: 21712  / 11 Years ago, sun, september 8, 2013, 12:00:00

In python doing this would be as simple as:


mylist = ['red','blue','green','yellow']
print(mylist[1:])
'blue','green','yellow'

When I tried this in JavaScript mylist[0] returned 'red' etc. but when I tried doing something like mylist[1:] in JavaScript it was not correct syntax.


What is a good method of doing this in JavaScript? Basically get all elements after a specified index, and to be specific, I am doing this in Node.js, but I do not think that matters much.


More From » node.js

 Answers
6

You can use the slice method like below:



mylist = ['red','blue','green','yellow'];
console.log(mylist.slice(1));


Output:



[blue, green, yellow] 


Demo | Array slice method - MDN Link


[#75832] Friday, September 6, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
oliverg

Total Points: 453
Total Questions: 101
Total Answers: 100

Location: Liechtenstein
Member since Wed, Dec 8, 2021
3 Years ago
;