【发布时间】:2018-02-15 08:02:29
【问题描述】:
我在对象中的 cmets 中的 cmets 中有 cmets(很像 droste 效果)。对于每条评论,都需要执行特定的操作。 cmets 可以无限期地继续下去。我应该如何循环它们?
例如
Comments
randcomment1
text: "Not important"
uid: 1234
Comments
randsubcomment1
text: "again ni"
uid: 5346
randsubcomment2
text: "ni"
uid: 9087
randcomment2
text: "N.I"
uid: 4567
我需要获取每条评论的 uid,使用它来调用数据库,然后将第三个键/值对添加到评论中。
例如
inidviualcomment
text: "ni"
uid: 4567
nickname: "Mr example" <------ this one should be added based on the uid
我目前拥有的
// using firebase and vuejs, not relevant
for (let key in val){
db.ref("users/" + val[key].uid).once("value").then(function(snapshot){
let value = snapshot.val()
let nickName = value.nickname
this.$set(val[key], "nickName", nickName)
}.bind(this))
//
// here you could add
// if (typeod val[key][Comments] != "undefined"){
// for (key in val[key][comments]){
// and so on, but this is not sustainable
// }
// }
//
}
我应该如何循环遍历 cmets,以使 cmets 中有多少 cmets 无关紧要?
【问题讨论】:
-
递归是最简单的方法
标签: javascript loops object indefinite