feat(observable-array): findIndex now supported

This commit is contained in:
Nathan Walker
2021-03-17 13:19:38 -07:00
parent 14e88f6e9d
commit 770030e7f6
2 changed files with 33 additions and 0 deletions

View File

@@ -44,4 +44,28 @@ describe('observable-array', () => {
assert.equal(0, _array.length);
});
});
describe('findIndex', () => {
it('finds an item', () => {
const _array = new ObservableArray();
_array.push(1);
_array.push(2);
const index = _array.findIndex((i) => i === 2);
assert.equal(1, index);
});
it('does not find item', () => {
const _array = new ObservableArray();
_array.push(1);
_array.push(2);
const index = _array.findIndex((i) => i === 3);
assert.equal(-1, index);
});
});
});

View File

@@ -288,6 +288,15 @@ export class ObservableArray<T> extends Observable {
return result;
}
/**
* Returns the index of the first element in the array where predicate is true, and -1 otherwise.
* @param predicate
* @param thisArg If provided, it will be used as the this value for each invocation of predicate. If it is not provided, undefined is used instead.
*/
findIndex(predicate: (value: any, index: number, obj: any[]) => unknown, thisArg?: any): number {
return this._array.findIndex(predicate, thisArg);
}
/**
* Returns the index of the first occurrence of a value in an array.
* @param searchElement The value to locate in the array.