Issue
I have the following extract of code:
private getNextFakeLinePosition(startPosition: number): number{
return this.models.findIndex(m => m.fakeObject);
}
This function returns me the index of the first element which has the property fakeObject
with the true value.
What I want is something like this, but instead of looking for the all items of the array, I want to start in a specific position (startPosition
).
Note: This is typescript but the solution could be in javascript vanilla.
Thank you.
Solution
You can try with slice
:
private getNextFakeLinePosition(startPosition: number): number {
const index = this.models.slice(startPosition).findIndex(m => m.fakeObject);
return index === -1 ? -1 : index + startPosition;
}
It'll slice your input array and find the index on a subarray. Then - at the end, just add the startPosition
to get the real index.
Answered By - hsz
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.