Last active
January 8, 2023 03:18
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example: