Skip to content

Instantly share code, notes, and snippets.

@hiulit
Last active January 8, 2023 03:18
Show Gist options
  • Save hiulit/110d7ba06aa2dd3a79d8272a31e601b2 to your computer and use it in GitHub Desktop.
Save hiulit/110d7ba06aa2dd3a79d8272a31e601b2 to your computer and use it in GitHub Desktop.
Filter an array that includes every/some of the elements of a given array
func filter_includes(type: String, base_array: Array, filter_array: Array, filter_key: String = "") -> Array:
var filtered_array := []
for base_item in base_array:
var count = 0
var search_item = base_item
if filter_key:
search_item = base_item[filter_key]
if type == "every":
for filter_item in filter_array:
if filter_item in search_item:
count += 1
if count == filter_array.size():
filtered_array.append(base_item)
elif type == "some":
for filter_item in filter_array:
if filter_item in search_item:
filtered_array.append(base_item)
break
return filtered_array
@hiulit
Copy link
Author

hiulit commented Feb 14, 2022

I've updated the function. Thanks to jeyzu on Reddit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment