如何根据字符串数组过滤数组数组?
如果数组中的部分或全部项目存在于另一个数组中,是否有一个some或any等效的 JS/Py 在哪里我可以过滤数组数组?
因此,例如,将其视为源数组:
arrays := [][]string{
{"some", "value"},
{"some", "value", "another"},
{"value", "another", "test"},
{"value", "test"},
{"some", "test"},
}
如果在数组中找到这里的所有项目arrays,我想过滤。[]string{"some", "value"}
预期的输出是
[[some value] [some value another]]
或者,如果我将过滤器更改为[]string{"some", "test"},则预期值为[[some test]]
我可以在我的测试代码中完全正确地理解逻辑
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)
}
慕仙森
汪汪一只猫
萧十郎
相关分类