Typescript Array find element by index

Finding and assigning a CategoryApi object having categoryId

export class CategoryApi {
    categoryId: number;
    name: string;

categories: CategoryApi[];
selectedCategory: CategoryApi;

selectedCategory = categories.findByIndex(2); //looking for categoryApi by categoryId = 2

I found this javascript option but Typescript is complaining about function

var inventory = [
    {name: 'apples', quantity: 2},
    {name: 'bananas', quantity: 0},
    {name: 'cherries', quantity: 5}

function findCherries(fruit) { 
    return fruit.name === 'cherries';

console.log(inventory.find(findCherries)); // { name: 'cherries', quantity: 5 }

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];

Array.prototype.findIndex(), searchElement − Element to locate in the array. fromIndex − The index at which to begin the search. Defaults to 0, i.e. the whole array will be searched. If the� indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present. Syntax array.indexOf(searchElement[, fromIndex]); Parameter Details. searchElement − Element to locate in the array. fromIndex − The index at which to begin the search. Defaults to 0, i.e. the whole array will be searched.

TO get specific parameter values. Use this code

this.categories.find(item => item.categoryId === 1).categoryId 

WHere categoryId can be any field you want

Typescript Array find element by index, An array in TypeScript can contain elements of different data types using a generic array The array index starts from zero, so the index of the first element is zero, the index of includes(), Checks whether the array contains a certain element. The Array.indexOf () is an inbuilt TypeScript function which is used to find the index of the first occurrence of the search element provided as the argument to the function.

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:

  1. change find with filter and press F12 (go to declaration)
  2. copy this line in the typings-file and rename the function to find and the returned value to single-object instead of an array!
  3. save that file
  4. rename your back filter to find in your code..

TypeScript - Array indexOf(), Example. Get the index of the first element in the array that has a value of 18 or more: var ages = [3, 10, 18, 20]; function checkAdult(age) { return age >= 18; typescript documentation: Finding Object in Array. RIP Tutorial. en English (en) Français (fr) TypeScript Finding Object in Array Example Using find()

TypeScript - Arrays, In JavaScript, there are plenty of useful ways to find items in Arrays. You could Do you need the index of the element or the element itself? You can use the splice method on an array to remove the elements. for example if you have an array with the name arr use the following: arr.splice(2, 1); so here the element with index 2 will be the starting point and the argument 2 will determine how many elements to be deleted. If you want to delete the last element of the array named arr

TypeScript Arrays, log("Wonder Woman's rank is " + rank);. The indexOf method will return the index of the first element which matches your search. Or if it does not find a� If you don't know the index of the item, you can use array.indexOf (item) to get it (in a if () to get one item or in a while () to get all of them). array.indexOf (item) returns either the index or -1 if not found. It's kinda funny that splice returns another array built out of the removed elements.

JavaScript Array findIndex() Method, find array item with index of 1. 4. const atIndex = items.find(function(element, index){. 5. The findIndex () method executes the function once for each element present in the array: If it finds an array element where the function returns a true value, findIndex () returns the index of that array element (and does not check the remaining values) Otherwise it returns -1

  • Most simplest approach would be this selectedCategory = categories.forEach(catApi => { if(catApi.id == requiredId) return catApi})