You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
868 B

4 years ago
  1. # `splice()`
  2. The splice method removes and returns a slice of items starting at the specified index:
  3. ```js
  4. const collection = collect([1, 2, 3, 4, 5]);
  5. const chunk = collection.splice(2);
  6. chunk.all();
  7. // [3, 4, 5]
  8. collection.all();
  9. // [1, 2]
  10. ```
  11. You may pass a second argument to limit the size of the resulting chunk:
  12. ```js
  13. const collection = collect([1, 2, 3, 4, 5]);
  14. const chunk = collection.splice(2, 1);
  15. chunk.all();
  16. // [3]
  17. collection.all();
  18. // [1, 2, 4, 5]
  19. ```
  20. In addition, you can pass a third argument containing the new items to replace the items removed from the collection:
  21. ```js
  22. const collection = collect([1, 2, 3, 4, 5]);
  23. const chunk = collection.splice(2, 1, [10, 11]);
  24. chunk.all();
  25. // [3]
  26. collection.all();
  27. // [1, 2, 10, 11, 4, 5]
  28. ```
  29. [View source on GitHub](https://github.com/ecrmnn/collect.js/blob/master/src/methods/splice.js)