0 votes

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': "idmain3"}

Only those with the id idmain3

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]
Godot version 4.0
in Engine by (30 points)

1 Answer

0 votes
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
by (185 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.