Array.prototype.findIndex() and Array.prototype.findLastIndex() are intended for more complex predicate checks.
When the callback only performs a simple strict equality check (x === value), .indexOf() or .lastIndexOf() are more readable and expressive.
This rule reports when .findIndex() or .findLastIndex() can be simplified.
Array<string>.findIndex(predicate: (value:string, index:number, obj:string[])=> unknown, thisArg?: any): number
Returns the index of the first element in the array where predicate is true, and -1
otherwise.
@param ― predicate find calls predicate once for each element of the array, in ascending
order, until it finds one where predicate returns true. If such an element is found,
findIndex immediately returns that element index. Otherwise, findIndex returns -1.
@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((
item: string
item)=>
item: string
item==="value");
declare const
const array:string[]
array:string[];
const array:string[]
array.
Array<string>.findIndex(predicate: (value:string, index:number, obj:string[])=> unknown, thisArg?: any): number
Returns the index of the first element in the array where predicate is true, and -1
otherwise.
@param ― predicate find calls predicate once for each element of the array, in ascending
order, until it finds one where predicate returns true. If such an element is found,
findIndex immediately returns that element index. Otherwise, findIndex returns -1.
@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((
item: string
item)=>"value"===
item: string
item);
declare const
const array:string[]
array:string[];
const array:string[]
array.
Array<string>.findLastIndex(predicate: (value:string, index:number, array:string[])=> unknown, thisArg?: any): number
Returns the index of the last element in the array where predicate is true, and -1
otherwise.
@param ― predicate findLastIndex calls predicate once for each element of the array, in descending
order, until it finds one where predicate returns true. If such an element is found,
findLastIndex immediately returns that element index. Otherwise, findLastIndex returns -1.
@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.
findLastIndex((
item: string
item)=>
item: string
item==="value");
declare const
const array:string[]
array:string[];
const array:string[]
array.
Array<string>.findIndex(predicate: (value:string, index:number, obj:string[])=> unknown, thisArg?: any): number
Returns the index of the first element in the array where predicate is true, and -1
otherwise.
@param ― predicate find calls predicate once for each element of the array, in ascending
order, until it finds one where predicate returns true. If such an element is found,
findIndex immediately returns that element index. Otherwise, findIndex returns -1.
@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(function(
item: string
item) {
return
item: string
item==="value";
});
declare const
const array:string[]
array:string[];
const array:string[]
array.
Array<string>.indexOf(searchElement: string, fromIndex?: number): number
Returns the index of the first occurrence of a value in an array, or -1 if it is not present.
@param ― searchElement The value to locate in the array.
@param ― fromIndex The array index at which to begin the search. If fromIndex is omitted, the search starts at index 0.
indexOf("value");
declare const
const array:string[]
array:string[];
const array:string[]
array.
Array<string>.lastIndexOf(searchElement: string, fromIndex?: number): number
Returns the index of the last occurrence of a specified value in an array, or -1 if it is not present.
@param ― searchElement The value to locate in the array.
@param ― fromIndex The array index at which to begin searching backward. If fromIndex is omitted, the search starts at the last index in the array.
lastIndexOf("value");
declare const
const array:string[]
array:string[];
const array:string[]
array.
Array<string>.findIndex(predicate: (value:string, index:number, obj:string[])=> unknown, thisArg?: any): number
Returns the index of the first element in the array where predicate is true, and -1
otherwise.
@param ― predicate find calls predicate once for each element of the array, in ascending
order, until it finds one where predicate returns true. If such an element is found,
findIndex immediately returns that element index. Otherwise, findIndex returns -1.
@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.
Returns true if the sequence of elements of searchString converted to a String is the
same as the corresponding elements of this object (converted to a String) starting at
position. Otherwise returns false.
startsWith("v"));
declare const
const array:number[]
array:number[];
const array:number[]
array.
Array<number>.findIndex(predicate: (value:number, index:number, obj:number[])=> unknown, thisArg?: any): number
Returns the index of the first element in the array where predicate is true, and -1
otherwise.
@param ― predicate find calls predicate once for each element of the array, in ascending
order, until it finds one where predicate returns true. If such an element is found,
findIndex immediately returns that element index. Otherwise, findIndex returns -1.
@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((
item: number
item)=>
item: number
item>0);
declare const
const array:object[]
array:object[];
const array:object[]
array.
Array<object>.findIndex(predicate: (value:object, index:number, obj:object[])=> unknown, thisArg?: any): number
Returns the index of the first element in the array where predicate is true, and -1
otherwise.
@param ― predicate find calls predicate once for each element of the array, in ascending
order, until it finds one where predicate returns true. If such an element is found,
findIndex immediately returns that element index. Otherwise, findIndex returns -1.
@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((
item: object
item)=>
item: object
item.id===1);
Error ts(2339) ― Property 'id' does not exist on type 'object'.
If you prefer the functional style of .findIndex() for consistency, or if your codebase has a large number of existing uses, you may disable this rule.