從火力地堡我的目標,當我CONSOLE.LOG:檢索嵌套對象的JavaScript
Object {AW: Object, Qdoba: Object}
然後下的AW和Qdoba餐館冠軍,我有標題和地址,我可以擴大和看到我的控制檯。我想在我的網頁上顯示來自兩家餐廳的所有數據。
如何在不知道AW
和Qdoba
的情況下訪問兩個餐廳對象?我的代碼如下。
// Initialize Firebase
var config = {
apiKey: "xxxxxxxx",
authDomain: "xxxxxxxx",
databaseURL: "xxxxxxxx",
storageBucket: "xxxxx.xxxxxx.com",
messagingSenderId: "xxxxx"
};
firebase.initializeApp(config);
var firebaseRef = firebase.database().ref('listings/');
//load
firebaseRef.on("value", function(snapshot) {
document
.querySelector('#contacts')
.innerHTML += contactHtmlFromObject(snapshot.val());
});
//prepare object's HTML
function contactHtmlFromObject(item){
console.log(item)
var html = '';
html += '<li class="list-group-item contact">';
html += '<div>';
html += '<p class="lead">'+item.title+'</p>';
html += '<p>'+item.title+'</p>';
html += '<p><small title="'
+item.title+'">'
+item.address
+'</small></p>';
html += '</div>';
html += '</li>';
return html;
}
我的火力地堡設置:
{
"listings" : {
"A&W" : {
"active" : true,
"address" : "3939",
"description" : "Super good root beer",
"title" : "A&W"
},
"Qdoba" : {
"active" : true,
"address" : "1234 main",
"description" : "A really good place to eat",
"title" : "Gellas"
}
}
}
'for(var object.listings中的餐廳)' – Barmar