替代数据快照的foreach循环,以便在满足条件时退出循环



我目前正在运行一个forEach循环,如下所示:

snapshot.forEach((theChild) => 
let newPostID1 = String(snapshoot.key);
let newUID = String(child.key);
//Spill coffee here.
})

ForEach循环不允许在满足条件后退出。

在快照中循环的另一种方法是什么,它允许我在满足条件后返回(退出(

您可以使用val((在快照中获取一个包含整个数据集的JavaScript对象。然后,您可以使用for的标准JavaScript对象属性迭代来迭代其键。。你想什么时候进去就什么时候休息。

const val = snapshot.val()
for (const key in val) {
console.log(`child key: ${key}`)
console.log(`child value: ${val[key]}`)
// break whenever you want
}

最新更新