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.

33 lines
822 B

4 years ago
  1. # `prepend()`
  2. The prepend method adds an item to the beginning of the collection:
  3. ```js
  4. const collection = collect([1, 2, 3, 4, 5]);
  5. collection.prepend(0);
  6. collection.all();
  7. // [0, 1, 2, 3, 4, 5]
  8. ```
  9. You may also pass a second argument to set the key of the prepended item:
  10. > Pro tip: Order of properties in objects is not guaranteed in JavaScript; When calling prepend with a key, the Collection uses the underlying `put` method behind the scenes. This is only supported so that collect.js have the same api as Laravel Collections.
  11. ```js
  12. const collection = collect({
  13. product: 'iPhone 6s',
  14. });
  15. collection.prepend('Apple', 'brand');
  16. collection.all();
  17. // {
  18. // brand: 'Apple',
  19. // product: 'iPhone 6s',
  20. // }
  21. ```
  22. [View source on GitHub](https://github.com/ecrmnn/collect.js/blob/master/src/methods/prepend.js)