books
对象的键由Firebase自动生成。
{
books: {
-LKsSspLai8tTppJjSiE: {
author: 'Author 1',
name: 'Book 1'
},
-LKsS15TMhGRQXlZZlkE: {
author: 'Author 2',
name: 'Book 2'
},
{}, {}...
}
timestamp: 1535579350
}
访问一级属性没有问题,但是如何从books
对象创建一个数组以进一步遍历authors
和names
?
答案 0 :(得分:2)
您可以使用Object.values(books),然后遍历该数组。
Object.values(books).forEach( (book) => {
// now you have book.author and book.name
});
答案 1 :(得分:0)
如果要将对象的值转换为数组(按要求),则可以使用lodash:
const a = {
books: {
"-LKsSspLai8tTppJjSiE": {
author: 'Author 1',
name: 'Book 1'
},
"-LKsS15TMhGRQXlZZlkE": {
author: 'Author 2',
name: 'Book 2'
},
},
timestamp: 1535579350
}
console.log( _.values(a.books) )
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.js"></script>