mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
feat(observable-array): findIndex now supported
This commit is contained in:
@@ -44,4 +44,28 @@ describe('observable-array', () => {
|
|||||||
assert.equal(0, _array.length);
|
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);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -288,6 +288,15 @@ export class ObservableArray<T> extends Observable {
|
|||||||
return result;
|
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.
|
* Returns the index of the first occurrence of a value in an array.
|
||||||
* @param searchElement The value to locate in the array.
|
* @param searchElement The value to locate in the array.
|
||||||
|
|||||||
Reference in New Issue
Block a user