我有一个从 firebase 接收的简单数据,但过滤器不适用于该数据。 我搜索了很多帖子,但没有任何帮助。 缺少什么。?任何帮助
如果它在 OnInit() 中,我可以获得相同的数据。 下面是我的代码:
export class SignUpComponent implements OnInit {
ngOnInit() {
const someEvemnts = [
{type: 'attack', value: 1, target: 'dorkman'},
{type: 'yaeed', value: 2, target: 'dorkman'},
{type: 'attack', value: 3, target: 'fluffy'},
{type: 'attack', value: 4, target: 'dorkman'},
]
const total = someEvemnts
.filter(event => event.type== 'attack')
.filter(event => event.target== 'dorkman')
console.log(total); // works fine
let check = this.values; // works fine
let check2 = this.values.filter(val => val) // not works
console.log(check);
console.log(check2); // returns empty
this.SignIn();
}
SignIn(){
firebase.database().ref('/companies').on('child_added', (data)=>{
this.values.push(data.val())
}
)
}
values=[];
}
控制台:
.
最佳答案
它不起作用,因为您调用了 this.SignIn();在调用 console.log 后填充“Values”变量。因此,当您进行控制台操作时,“值”为空。
变量流行后执行“console.log”。
试试这个:
export class SignUpComponent implements OnInit {
ngOnInit() {
const someEvemnts = [
{type: 'attack', value: 1, target: 'dorkman'},
{type: 'yaeed', value: 2, target: 'dorkman'},
{type: 'attack', value: 3, target: 'fluffy'},
{type: 'attack', value: 4, target: 'dorkman'},
]
const total = someEvemnts
.filter(event => event.type== 'attack')
.filter(event => event.target== 'dorkman')
console.log(total); // works fine
this.SignIn();
}
SignIn(){
firebase.database().ref('/companies').on('child_added', (data)=>{
this.values.push(data.val())
let check = this.values; // works fine
let check2 = this.values.filter(val => val) // not works
console.log(check);
console.log(check2); // returns empty
}
)
}
values=[];
}
关于javascript - 过滤器不能以 Angular 工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47904082/