+ 1

How can i select multiple indexes of an array/string with JS?

Python has things like "[3:]","[1:5]" and "[::-1]". How can I get similar results in JS?

13th Jan 2017, 9:59 PM
IgorSM
IgorSM - avatar
3 odpowiedzi
+ 3
I don't know Python, but I will list all the similar methods for JS. var str = "SoloLearn" ; str.substr(2, 3) // starting from the second index, selects 3, outputs "loL" str.substring(2, 6) // starting from the second index, selects up to but not including the sixth index, outputs "loLe" str.slice(2, 6) // the same as substring str.slice(2) // selects from the second index to the last index, outputs "loLearn" str.slice(2, -2) // selects from the second index, selects up to but not including the second last index, outputs "loLea" var arr = ["Apple", "Pen, "Pineapple", "Pen"] arr.splice(1, 2) // starting from the first index, selects 2, outpus "Pen, Pineapple"
14th Jan 2017, 1:14 AM
David Sebastian Keshvi Illiakis
David Sebastian Keshvi Illiakis - avatar
+ 2
I'm glad to help
14th Jan 2017, 2:22 AM
David Sebastian Keshvi Illiakis
David Sebastian Keshvi Illiakis - avatar
+ 1
PPAP 😁 But thx, you really really helped me! I was trying to use For Loops, it clearly didn't work lol Again, thx, you saved a life today
14th Jan 2017, 2:22 AM
IgorSM
IgorSM - avatar