【发布时间】:2017-09-16 13:22:19
【问题描述】:
如何使用箭头函数缩短 for...of 语法?
this.id = 1;
let products: Product[] = [
{
"id": 1,
"name": "Bycicle"
},
{
"id": 2,
"name": "iPhoneX"
}
];
for (let p of products) {
if (p.id == this.id) {
this.product = p;
break;
}
}
最后一个块可以写成单行吗?我试过了,但它看起来很不对:
this.product = products.filter(p => p.id == this.id)[0];
我在 .NET 中寻找类似 .FirstOrDefault 的东西
【问题讨论】:
标签: javascript typescript ecmascript-6 arrow-functions