Typescript Array find element by index
There is no such method findByIndex in JS, the only method is findIndex.
You can use the filter or the find method to get the item by index:
// ES2015
selectedCategory = categories.find(item => item.categoryId === 1);
// ES5
selectedCategory = categories.filter(item => item.categoryId === 1)[0];
You can use the find method:
selectedCategory = categories.find(c => c.categoryApi == 2);
The problem is, that the find
function isn't listed in the typings-file.
First, ignore the error-message and try it!
Second, you could edit the typings-file:
- change
find
withfilter
and press F12 (go to declaration) - copy this line in the typings-file and rename the function to
find
and the returned value to single-object instead of an array! - save that file
- rename your back
filter
tofind
in your code..
TO get specific parameter values. Use this code
this.categories.find(item => item.categoryId === 1).categoryId
WHere categoryId can be any field you want