Filter a dictionary with a dictionary, which is inside an array of dictionaries?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By zkmark

How to filter a dictionary with a dictionary, which is inside an array of dictionaries?

I have several dictionaries inside an array and I would like to filter them by id like this

{‘id’: “id_main_3”}

Only those with the id id_main_3

var dictionary2 = {
	"id": "main_2",
	"name": "Main 2",
	"key": 'value'
}

var dictionary3 = {
	"id": "id_main_3",
	"id3": "main_3",
	"name3": "Main 3",
	"key": 'value',
	"other": 'value'
}


var dicts: Array = [dictionary2, dictionary3]
:bust_in_silhouette: Reply From: alexp
func filter_by_id(dicts: Array, id: String) -> Array:
  var result = []
  for d in dicts:
    if not d.has("id"):
      continue
    if d["id"] == id:
      result.append(d)
  return result