@walts81/linq-ts
Version:
Typescript/Javascript LINQ implementation library
18 lines (17 loc) • 536 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.firstOrDefault = void 0;
Array.prototype.firstOrDefault = firstOrDefault;
function firstOrDefault(expression, defaultValue = null) {
const length = this.length;
if (!expression)
return length > 0 ? this[0] : defaultValue;
for (let i = 0; i < length; i++) {
const item = this[i];
if (expression(item, i)) {
return item;
}
}
return defaultValue;
}
exports.firstOrDefault = firstOrDefault;