gpt4 book ai didi

javascript - 如何在 Firebase 中循环浏览快照

转载 作者:行者123 更新时间:2023-12-01 00:36:54 25 4
gpt4 key购买 nike

我的firebase结构如下:

enter image description here

以下代码搜索用户在选择框中选择的文本 (2019/9/7 AMS MK7777) 并返回 ulds 对象

FBLoad: function(){
console.log($(".text").text())

var ref = firebase.database().ref("flights");
ref
.orderByChild("queryDes")
.equalTo($(".text").text())
.once("child_added", function(snapshot) {
console.log(snapshot.val().ulds);
});
}

我想要完成的是对象的数据由来自 ulds 对象的数据更新需要更新的对象如下:

var positions =  {
"A": {h_arm:14645, "occupied":0, "ULD":"", "DEST":"", "CNTR":"", "WGHT":0, "TYPE":"", "SHC":[], "RMRK":""},
"BR": {h_arm:16905, "occupied":0, "ULD":"", "DEST":"", "CNTR":"", "WGHT":0, "TYPE":"", "SHC":[], "RMRK":""},
"C": {h_arm:19165, "occupied":0, "ULD":"", "DEST":"", "CNTR":"", "WGHT":0, "TYPE":"", "SHC":[], "RMRK":""},
"R": {h_arm:19165, "occupied":0, "ULD":"", "DEST":"", "CNTR":"", "WGHT":0, "TYPE":"", "SHC":[], "RMRK":""}
}

最终结果应该是这样的:

var positions =  {
"A": {h_arm:14645, "occupied":0, "ULD":"", "DEST":"", "CNTR":"", "WGHT":0, "TYPE":"", "SHC":[], "RMRK":""},
"BR": {h_arm:16905, "occupied":0, "ULD":"PMC12345QR", "DEST":"DOH", "CNTR":"Q5", "WGHT":2500, "TYPE":"C", "SHC":["RFL","RCM"], "RMRK":"OHG75CMF"},
"C": {h_arm:19165, "occupied":0, "ULD":"", "DEST":"", "CNTR":"", "WGHT":0, "TYPE":"", "SHC":[], "RMRK":""},
"R": {h_arm:19165, "occupied":0, "ULD":"PMC77785QR", "DEST":"DOH", "CNTR":"Q5", "WGHT":3540, "TYPE":"C", "SHC":["PIL"], "RMRK":"IND75CMA"}
}

我找不到循环 ulds-object

最佳答案

要循环遍历 uld,请尝试以下操作:

once("value", function(snapshot) {
snapshot.forEach(function(childSnapshot){
childSnapshot.forEach(function(uldSnapshot){
uldSnapshot.forEach(function(nestedUldSnapshot){
console.log(nestedUldSnapshot.val());

});
});

首先使用事件value获取数据,然后使用forEach()获取随机id下的数据。然后使用另一个 forEach() 访问 ulds 对象内的数据。

关于javascript - 如何在 Firebase 中循环浏览快照,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58049503/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com