mirror of
https://github.com/samber/lo.git
synced 2026-04-22 15:37:14 +08:00
fa095e4b4f
* fix(doc): fix go playground demo URL * fix(doc): add more go playground demo URL
32 lines
570 B
Markdown
32 lines
570 B
Markdown
---
|
|
name: CountBy
|
|
slug: countby
|
|
sourceRef: slice.go#L849
|
|
category: core
|
|
subCategory: slice
|
|
playUrl: https://go.dev/play/p/5GMQP5vNT4q
|
|
variantHelpers:
|
|
- core#slice#countby
|
|
similarHelpers:
|
|
- core#slice#count
|
|
- core#slice#every
|
|
- core#slice#some
|
|
- core#slice#filter
|
|
- core#slice#find
|
|
- core#slice#countbyerr
|
|
position: 0
|
|
signatures:
|
|
- "func CountBy[T any](collection []T, predicate func(item T) bool) int"
|
|
---
|
|
|
|
Counts the number of elements for which the predicate is true.
|
|
|
|
```go
|
|
lo.CountBy([]int{1, 5, 1}, func(i int) bool {
|
|
return i < 4
|
|
})
|
|
// 2
|
|
```
|
|
|
|
|