Files
30-seconds-of-code/snippets/drop-right.md
2017-12-18 19:12:51 +01:00

377 B

Array dropRight

Returns a new array with n elements removed from the right

Checks if n is shorter than the given array and slices it accordingly or returns an empty array

const dropRight = (arr, n = 1) => (arr.length - n ) > 0 ? arr.slice(0, arr.length - n) : []

//dropRight([1,2,3]) -> [1,2]
//dropRight([1,2,3], 2) -> [1]
//dropRight([1,2,3], 42) -> []