_.takeRight

Creates a slice of array with n elements taken from the end.❗️ Native slice does not behave entirely the same as the Lodash method. See example below to understand why.

  1. // Underscore/Lodash
  2. _.takeRight([1, 2, 3]);
  3. // => [3]
  4.  
  5. _.takeRight([1, 2, 3], 2);
  6. // => [2, 3]
  7.  
  8. _.takeRight([1, 2, 3], 5);
  9. // => [1, 2, 3]
  10.  
  11. // Native
  12. [1, 2, 3].slice(-1);
  13. // => [3]
  14.  
  15. [1, 2, 3].slice(-2);
  16. // => [2, 3]
  17.  
  18. [1, 2, 3].slice(-5);
  19. // => [1, 2, 3]
  20.  
  21. // Difference in compatibility
  22.  
  23. // Lodash
  24. _.takeRight([1, 2, 3], 0);
  25. // => []
  26.  
  27. // Native
  28. [1, 2, 3].slice(0);
  29. // => [1, 2, 3]

Browser Support for Array.prototype.slice()

ChromeEdgeFirefoxIEOperaSafari
1.0 ✔1.0 ✔