How to filter arrays

Godot Version

godot4

Question

how can I take this
arr = [a, b, c, c, d]
and turn it into this by filtering out c
[a, b, d]

You can use the Array.filter(method) function for this. You would need something like this:

arr.filter(func(element): return element != c)
1 Like

thnx for the help. im not quite smart enough for that though so i ended up just making a function. tysm

func filter_arr(_arr, _filter_val):
	var filteredArr = []
	for i in _arr.size():
		if _arr[i] != _filter_val:
			filteredArr.append(_arr[i])
	return filteredArr
1 Like

Don’t bring yourself down, you can do anything you put your mind to!

1 Like

thnx the godot community is so positive