Angular2-过滤对象列表。即使比较结果为真,结果列表也为空

Angular2-过滤对象列表。即使比较结果为真,结果列表也为空,angular,typescript,Angular,Typescript,让Reference是这样一个类: export class Reference { id : number; code : string; label : string; constructor(id : number, code : string, label : string { this.id = id; this.code = code; this.label = label; } } 让我们让

让Reference是这样一个类:

export class Reference {
    id : number;
    code : string;
    label : string;

    constructor(id : number, code : string, label : string {
        this.id = id;
        this.code = code;
        this.label = label;
    }
}
让我们让一个使用这个引用类型的组件,有一个引用dataList数组和另一个引用selectedReference,它将等于数组中的一个元素,表示相同的属性值,而不是相同的对象。我想获得数组中属性id等于selectedReference的元素,所以我过滤列表。这是课程的代码:

export class filterExample {
    private dataList = Array<Reference>(); 
    private selectedValue : Reference = new Reference(2, 'R2', 'Reference 2');

    constructor() {}

    ngOnInit() { 
        for (let i : number = 1; i <= 3; i++) {
            let reference : Reference = new Reference(i, `R${i}`, `Reference ${i}`);
            this.dataList.push(reference);
        }
        let element = this.dataList.filter((item : Reference) => {
            console.log(item.id === this.selectedValue.id);
            item.id === this.selectedValue.id;
        });    
        console.log(element);
    };
}
好吧,我试着通过做以下事情来简化它:

let element = this.dataList.filter((item : Reference) => {
    console.log(item.id;
    item.id === 2;
});     
console.log(element);  
控制台输出现在显示:

1
2
3
[]
即使我做了一些傻事,比如:

let element = this.dataList.filter((item : Reference) => {
    true;
});     
console.log(element);  
结果也是一个空数组

我做错了什么?
提前感谢

您不会从谓词函数返回任何内容:

return item.id === this.selectedValue.id;
 ^-- add this.
如果没有块,但箭头后面有表达式,则只能忽略返回。仅在这种情况下,返回是隐式的:

this.dataList.filter(item  => item.id === this.selectedValue.id);
此外,过滤数组会返回一个数组。不是谓词接受的第一个元素。应该如此

let element = this.dataList.filter(item => item.id === this.selectedValue.id)[0];

工作!谢谢,我不知道区块中的非隐式回报:
let element = this.dataList.filter(item => item.id === this.selectedValue.id)[0];