我从 firebase 中提取一个对象,将其转换为数组,然后对其进行删除元素操作,但有一些奇怪的行为:
this.players = this.db.object('arena/players').valueChanges();
this.players.subscribe(players =>{
console.log(players);
this.playersarray= Object.keys(players).map(key => ({ key, value: players[key] }));
console.log(this.playersarray);
console.log(this.playersarray.length);
for(var i =0;i<this.playersarray.length;i++){
if(this.playersarray[i].value != "waiting"){
console.log(this.playersarray[i].value+"deleting");
this.playersarray.splice(i,1);
}
}
console.log(this.playersarray);
});
我正在尝试删除值不等于 waiting 的元素。因此,在这种情况下,我希望删除 engincan、lifesuxtr 并仅以 someotheruser、someuser 的形式获取最后一个 console.log 但lifesuxtr没有被删除???只有 engincan 被删除?
最佳答案
您可以使用Array.filter
运算符迭代数组并过滤相关结果,例如:
const arr = [1, 2, 3, 4, 5];
const result = arr.filter((item) => item % 2 === 1);
console.log(result);
关于javascript - 从 firebase 提取数据后从数组中删除元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48035725/