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.

51 lines
984 B

4 years ago
  1. # `contains()`
  2. The contains method determines whether the collection contains a given item:
  3. ```js
  4. const collection = collect({
  5. name: 'Steven Gerrard',
  6. number: 8,
  7. });
  8. collection.contains('name');
  9. // true
  10. collection.contains('age');
  11. // false
  12. collection.contains('Steven Gerrard');
  13. // true
  14. ```
  15. You may also work with arrays
  16. ```js
  17. const collection = collect([1, 2, 3]);
  18. collection.contains(3);
  19. // true
  20. ```
  21. You may also pass a key / value pair to the contains method, which will determine if the given pair exists in the collection:
  22. ```js
  23. const collection = collect({
  24. name: 'Steven Gerrard',
  25. number: 8,
  26. });
  27. collection.contains('name', 'Steve Jobs');
  28. // false
  29. ```
  30. Finally, you may also pass a callback to the contains method to perform your own truth test:
  31. ```js
  32. const collection = collect([1, 2, 3, 4, 5]);
  33. collection.contains((value, key) => value > 5);
  34. // false
  35. ```
  36. [View source on GitHub](https://github.com/ecrmnn/collect.js/blob/master/src/methods/contains.js)