我有一个以数字方式组织的列表/对象数组,我将对象名称视为' id'。我正在使用firebase,我无法弄清楚如何获取id号本身。任何帮助都会很棒
//js
myFirebaseRef.once("value", function(snapshot) {
var list = snapshot.val();
$scope.items = list
console.log($scope.items)
})
//log $scope.items
[1: Object, 2: Object, 3: Object, 4: Object, 5: Object, 6: Object, 7: Object //...
答案 0 :(得分:1)
与angular.forEach
类似,你可以做一些事情:
angular.forEach($scope.items,function(key,value){
//the key will contain your id
console.log(key);
})
答案 1 :(得分:1)
这是你要找的吗?
myFirebaseRef.once("value", function(snapshot) {
//var list = snapshot.val();
snapshot.forEach(function(itemSnapshot) {
console.log(itemSnapshot.key); // if you're using 2.x that is key()
});
})
答案 2 :(得分:0)
喜欢这个。您可以使用obj[key] = value;
为变量设置id。
function update(node,key,value){
var ref = firebase.database().ref('/');
var obj = {};
obj[key] = value;
ref.child(node).update(obj)
.then(function() {
console.log('Update Ran Successfully');
});
}
答案 3 :(得分:0)
这是一个真实的示例,使用react和axios检索firebase中对象的名称:
componentDidMount() {
axios.get('/uri.json')
.then(res => {
let fetchedData = [];
for (let key in res.data) {
fetchedData.push({
...res.data[key],
id: key //The key here holds the object's name !
});
}
});
}