Let say I have a array in this format:
[{"id":"11","children":[{"id":"9"},{"id":"5", "children":[{"id":"4"}]}]},{"id":"10","children":[{"id":"7"}]}]
And now I would like to get all ID's that are apparent in this array:
11,9,5,4,10,7
For that I would use a recursive code similar to this one:
ids = []
def find_ids arr
arr.each do |entry|
ids << entry["id"] if entry["id"]
find_ids(entry["children"]) if entry["children"]
end
end
What would you do to get the ids?
Do you maybe know a really short version?
Thanks
Aucun commentaire:
Enregistrer un commentaire