在Firebase的JSON树中为所有子项添加键和值(Add key and value to all childs in JSON tree in Firebase)

我想为Firebase中的所有子项添加键和值(“rate”:0)。

这是我的JSON树。 我如何循环通过JSON树并设置“rate”:0对所有这些如下:

我尝试用Pyrebase做这件事,但这很痛苦。

I want to add key and value to all childs in Firebase("rate":0).

This is my JSON tree. How I am able to loop trough the JSON tree and set "rate":0 to all of them like this:

I tried doing it with Pyrebase but it was a pain.

最满意答案

尝试:-

FIRDatabase.database().reference().child("Snuses").observeSingleEvent(of: .value, with: {(toBeAppendedSnap) in if let snapDict = toBeAppendedSnap.value as? [String:AnyObject]{ for each in snapDict{ let eachKey = each.key if let eachValue = each.value as? NSMutableDictionary{ eachValue.setObject("0", forKey: "rate" as NSCopying) FIRDatabase.database().reference().child("Snuses/\(eachKey)").setValue(eachValue, withCompletionBlock: { (err, ref) in print("Updated") }) } } } })

要么:-

FIRDatabase.database().reference().child("Snuses").observeSingleEvent(of: .value, with: {(toBeAppendedSnap) in if let snapDict = toBeAppendedSnap.value as? [String:AnyObject]{ for each in snapDict{ FIRDatabase.database().reference().child("Snuses/\(each.key)").updateChildValues(["rate" : "0"]) } } })

Try:-

FIRDatabase.database().reference().child("Snuses").observeSingleEvent(of: .value, with: {(toBeAppendedSnap) in if let snapDict = toBeAppendedSnap.value as? [String:AnyObject]{ for each in snapDict{ let eachKey = each.key if let eachValue = each.value as? NSMutableDictionary{ eachValue.setObject("0", forKey: "rate" as NSCopying) FIRDatabase.database().reference().child("Snuses/\(eachKey)").setValue(eachValue, withCompletionBlock: { (err, ref) in print("Updated") }) } } } })

Or:-

FIRDatabase.database().reference().child("Snuses").observeSingleEvent(of: .value, with: {(toBeAppendedSnap) in if let snapDict = toBeAppendedSnap.value as? [String:AnyObject]{ for each in snapDict{ FIRDatabase.database().reference().child("Snuses/\(each.key)").updateChildValues(["rate" : "0"]) } } })在Firebase的JSON树中为所有子项添加键和值(Add key and value to all childs in JSON tree in Firebase)

我想为Firebase中的所有子项添加键和值(“rate”:0)。

这是我的JSON树。 我如何循环通过JSON树并设置“rate”:0对所有这些如下:

我尝试用Pyrebase做这件事,但这很痛苦。

I want to add key and value to all childs in Firebase("rate":0).

This is my JSON tree. How I am able to loop trough the JSON tree and set "rate":0 to all of them like this:

I tried doing it with Pyrebase but it was a pain.

最满意答案

尝试:-

FIRDatabase.database().reference().child("Snuses").observeSingleEvent(of: .value, with: {(toBeAppendedSnap) in if let snapDict = toBeAppendedSnap.value as? [String:AnyObject]{ for each in snapDict{ let eachKey = each.key if let eachValue = each.value as? NSMutableDictionary{ eachValue.setObject("0", forKey: "rate" as NSCopying) FIRDatabase.database().reference().child("Snuses/\(eachKey)").setValue(eachValue, withCompletionBlock: { (err, ref) in print("Updated") }) } } } })

要么:-

FIRDatabase.database().reference().child("Snuses").observeSingleEvent(of: .value, with: {(toBeAppendedSnap) in if let snapDict = toBeAppendedSnap.value as? [String:AnyObject]{ for each in snapDict{ FIRDatabase.database().reference().child("Snuses/\(each.key)").updateChildValues(["rate" : "0"]) } } })

Try:-

FIRDatabase.database().reference().child("Snuses").observeSingleEvent(of: .value, with: {(toBeAppendedSnap) in if let snapDict = toBeAppendedSnap.value as? [String:AnyObject]{ for each in snapDict{ let eachKey = each.key if let eachValue = each.value as? NSMutableDictionary{ eachValue.setObject("0", forKey: "rate" as NSCopying) FIRDatabase.database().reference().child("Snuses/\(eachKey)").setValue(eachValue, withCompletionBlock: { (err, ref) in print("Updated") }) } } } })

Or:-

FIRDatabase.database().reference().child("Snuses").observeSingleEvent(of: .value, with: {(toBeAppendedSnap) in if let snapDict = toBeAppendedSnap.value as? [String:AnyObject]{ for each in snapDict{ FIRDatabase.database().reference().child("Snuses/\(each.key)").updateChildValues(["rate" : "0"]) } } })