将Firebase中的JSON树中的所有子项添加到键和值 [英] Add key and value to all childs in JSON tree in Firebase

查看:134
本文介绍了将Firebase中的JSON树中的所有子项添加到键和值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为Firebase中的所有子级添加键和值(rate:0)。
$ b 这是我的JSON树。我如何能够通过JSON树循环,并设置比率:0像这样的所有人:





我尝试过使用Pyrebase做这件事,但这是一件痛苦的事情。

解决方案


$ b

  FIRDatabase.database()。reference()。child(Snuses)。observeSingleEvent(of :.value:{(toBeAppendedSnap)in 

$ b如果让snapDict = toBeAppendedSnap.value as?[String:AnyObject] {

for snapDict {

let eachKey = each.key

if eachValue = each.value as?NSMutableDictionary {

eachValue.setObject(0, forKey:rateas NSCopying)
FIRDatabase.database()。reference()。child(Snuses / \(eachKey))。setValue(eachValue,with CompletionBlock:在
print(Updated)
})
}
}
}
})
中的{(err,ref)

或者:

 <$ c $如果让snapDict = toBeAppendedSnap()函数返回的值是

$ b, .value为? [String:AnyObject] {

在snapDict {

FIRDatabase.database()。reference()。child(Snuses / \(each.key) ).updateChildValues([rate:0])

}
}
})


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.

解决方案

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树中的所有子项添加到键和值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆