Raf A
Raf A

Reputation: 179

How would I iterate over all keys to get specific child value?

I am trying to iterate over all keys under "Timetable" to get the key value and Name of those that have an approved value of "Yes".

So for the following JSON structure:

Timetable

   Pikes_Lane_Primary_School_Bolton_UK
      Approved: Yes
      Name: Pikes Lane Primary School

   Essa_Academy_Bolton_UK
      Approved: No
      Name: Essa Academy

   Ladybridge_High_School_Bolton_UK
      Approved: Yes
      Name: Ladybridge High School

My desired output would be:

Pikes_Lane_Primary_School_Bolton_UK
Pikes Lane Primary School
Ladybridge_High_School_Bolton_UK
Ladybridge High School

This is the best I've managed to do over the last few hours:

        let schoolDatabase = Database.database().reference().child("Timetable")
        schoolDatabase.observeSingleEvent(of: .value, with: { (snapshot) in
            for child in snapshot.children {
                let schoolID = child as! DataSnapshot
                //print(schoolID.key)
                for grandchild in schoolID.children {
                    let varOne = grandchild as! DataSnapshot
                    print(varOne.key)
                }
            }
        })

This brings back the following:

Approved
Name
Approved
Name
Approved
Name

Upvotes: 1

Views: 163

Answers (2)

Raf A
Raf A

Reputation: 179

Finally got there in the end!

        let schoolDatabase = Database.database().reference().child("Timetable")
        schoolDatabase.observeSingleEvent(of: .value, with: { (snapshot) in
            for child in snapshot.children {
                let schoolID = child as! DataSnapshot
                let stringApproved = schoolID.childSnapshot(forPath: "Approved").value
                let stringSchoolName = schoolID.childSnapshot(forPath: "Name").value
                if stringApproved as? String == "Yes" {
                    print(schoolID.key)
                    print((stringSchoolName)!)
                    print((stringApproved)!)
                }
            }
        })

Upvotes: 0

Kamajabu
Kamajabu

Reputation: 506

    let schoolDatabase = Database.database().reference().child("Timetable")
    schoolDatabase
        .queryOrdered(byChild: "Approved")
        .queryEqual(toValue: "Yes")
        .observeSingleEvent(of: .value, with: { (snapshot) in
            let children = snapshot.children
                .compactMap { $0 as? DataSnapshot }

            children.forEach { tuple in
                print(tuple.key)

                if let tupleDictionary = tuple.value as? [String: Any] {
                    let name = tupleDictionary["Name"] as? String
                    print(name ?? "-")
                }
            }
        }
    )

Or if you are interested only in names (without key):

    let schoolDatabase = Database.database().reference().child("Timetable")
    schoolDatabase
        .queryOrdered(byChild: "Approved")
        .queryEqual(toValue: "Yes")
        .observeSingleEvent(of: .value, with: { (snapshot) in
            let children = snapshot.children
                .compactMap { $0 as? DataSnapshot }
                .compactMap { $0?.value as? [String: Any]}
                .compactMap { $0["Name"] as? String }


            children.forEach { name in
                print(name)
            }
        }
    )

Upvotes: 1

Related Questions