admin管理员组文章数量:1428252
How to iterate through nested firebase objects.
Publications-
|
|-Folder1------
| |-hdgjg76675e6r-
| | |-Name
| | |-Author
| |
| |+-hdgjdsf3275e6k
| |+-hd345454575e6f
|+-Folder2
In publications I have folders and in folders I have objects (containing properties like. Name, Author)
I have iterated through folders till now.
snapshot.forEach(function (snapshot) {
var key = snapshot.key();
var obj = snapshot.val();
console.log(key);
//output => Folder1 , Folder2 etc
});
When I print obj
console.log(obj);
It displays
How do I iterate through obj variable as it contains hdgjg76675e6r
, hdgjdsf3275e6k
etc and further?
How to iterate through nested firebase objects.
Publications-
|
|-Folder1------
| |-hdgjg76675e6r-
| | |-Name
| | |-Author
| |
| |+-hdgjdsf3275e6k
| |+-hd345454575e6f
|+-Folder2
In publications I have folders and in folders I have objects (containing properties like. Name, Author)
I have iterated through folders till now.
snapshot.forEach(function (snapshot) {
var key = snapshot.key();
var obj = snapshot.val();
console.log(key);
//output => Folder1 , Folder2 etc
});
When I print obj
console.log(obj);
It displays
How do I iterate through obj variable as it contains hdgjg76675e6r
, hdgjdsf3275e6k
etc and further?
3 Answers
Reset to default 5You obj
is just a normal javascript object, you can just use a simple for loop:
for(var key in obj) {
console.log(obj[key]);
}
or you can use again a forEach
on your snapshot:
folderSnapshot.forEach(function (objSnapshot) {
objSnapshot.forEach(function (snapshot) {
var val = snapshot.val();
console.log(val); // Should print your object
});
});
Will the tree increase in depth? If not, then the best solution here is to just do a double for loop.
snapshot.forEach(function (snapshot) {
var key = snapshot.key();
var obj = snapshot.val();
console.log(key);
//output => Folder1 , Folder2 etc
obj.forEach(function (book) {
var title = book.title;
var author = book.author;
});
});
No need to over-engineer things, in my opinion.
Heres simple way how you can get your object of objects to array of objects:
var myObject = {key1: {name: 'hello'}, key2: {name: 'hello2'}};
var myArrayOfObject = Object.values(myObject);
console.log(myArrayOfObject);
// Result: (2) [Object, Object]
本文标签: jqueryIterating through nested firebase objectsJavascriptStack Overflow
版权声明:本文标题:jquery - Iterating through nested firebase objects - Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745524344a2661767.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论