github.com/godaddy-x/freego@v1.0.156/goquery/iteration.go (about) 1 package goquery 2 3 // Each iterates over a Selection object, executing a function for each 4 // matched element. It returns the current Selection object. The function 5 // f is called for each element in the selection with the index of the 6 // element in that selection starting at 0, and a *Selection that contains 7 // only that element. 8 func (s *Selection) Each(f func(int, *Selection)) *Selection { 9 for i, n := range s.Nodes { 10 f(i, newSingleSelection(n, s.document)) 11 } 12 return s 13 } 14 15 // EachWithBreak iterates over a Selection object, executing a function for each 16 // matched element. It is identical to Each except that it is possible to break 17 // out of the loop by returning false in the callback function. It returns the 18 // current Selection object. 19 func (s *Selection) EachWithBreak(f func(int, *Selection) bool) *Selection { 20 for i, n := range s.Nodes { 21 if !f(i, newSingleSelection(n, s.document)) { 22 return s 23 } 24 } 25 return s 26 } 27 28 // Map passes each element in the current matched set through a function, 29 // producing a slice of string holding the returned values. The function 30 // f is called for each element in the selection with the index of the 31 // element in that selection starting at 0, and a *Selection that contains 32 // only that element. 33 func (s *Selection) Map(f func(int, *Selection) string) (result []string) { 34 for i, n := range s.Nodes { 35 result = append(result, f(i, newSingleSelection(n, s.document))) 36 } 37 38 return result 39 }