TL/DR
How can I filter an array of arrays in go against an array of strings?
Is there a some
or any
equivalent of JS/Py in go where I can filter an array of arrays if some or all the items in an array is present in another array?
So for example, consider this as the source array:
arrays := [][]string{
{"some", "value"},
{"some", "value", "another"},
{"value", "another", "test"},
{"value", "test"},
{"some", "test"},
}
And I want to filter arrays
by []string{"some", "value"}
if all the items here are found in the array.
The expected output is
[[some value] [some value another]]
Altervatively, if I change my filter to []string{"some", "test"}
, the expected value is [[some test]]
I can get the logic down quite right in my test code
package main
import "fmt"
func inArray(s string, arr []string) bool {
for _, a := range arr {
if s == a {
return true
}
}
return false
}
func main() {
arrays := [][]string{
{"some", "value"},
{"some", "value", "another"},
{"value", "another", "test"},
{"value", "test"},
{"some", "test"},
}
filterBy := []string{"some", "value"}
hold := make([][]string, 0)
// Ignore this because it doesnt work as expected
for _, arr := range arrays {
for _, f := range filterBy {
if ok := inArray(f, arr); ok {
hold = append(hold, arr)
}
}
}
fmt.Println(hold)
}