javascript

slice array from N to last element


How to make this transformation?

["a","b","c","d","e"] // => ["c", "d", "e"]

I was thinking that slice can do this, but..

["a","b","c","d","e"].slice(2,-1) // [ 'c', 'd' ]
["a","b","c","d","e"].slice(2,0)  // []

Solution

  • Don't use the second argument:

    Array.slice(2);
    

    https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/slice

    If end is omitted, slice extracts to the end of the sequence.