【发布时间】:2017-09-19 01:53:39
【问题描述】:
我有一个包含一些对象的数组。我想知道这个数组中有多少元素将“有效”字段设置为 true。
这是我现在的代码:
1) 数组元素对象:
export class Record {
public accuracy: string;
public executed: boolean;
public effective: boolean;
}
2) 数组:
public records: Array<Record>;
3)计算部分(这是我想知道如何使用Lodash改进的部分)
let sum: number = 0;
for (const record of this.records) {
if (record.effective) {
sum++;
}
}
从上面的代码可以看出,我打算做的是计算数组中“有效”字段设置为 true 的元素总数。
如何在 Lodash 中做到这一点?谢谢。
【问题讨论】:
-
可能只是
this.records.filter(record => record.effective).length但不使用 lodash
标签: arrays typescript lodash