-1

lets say we got an array and we want to get the 2-5 elements of it, is there a way we can do that like in python?

let test = ['a','b','c','d','e','f','g'];

in python we can do,

test[1:4]

and this will return us the 2nd to 5th elements does node offer a simple way to do this that way?

  • There is [`.slice()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice) – Nick Parsons Jun 30 '22 at 10:41

1 Answers1

0

You can just use the slice method:

let test = ['a','b','c','d','e','f','g'];
test.slice(1,4) // ['b', 'c', 'd' ]
geekonaut
  • 5,714
  • 2
  • 28
  • 30