Navigate back to the homepage

How to get array index value in typescript?

February 3rd, 2022 · 1 min read
keep affilitate

Hello Friends 👋,

Welcome To Infinitbility! ❤️

To get index value of element typescript have indexOf() and findIndex() method but both have different poupose of use.

Today, we will learn how to get index array elements in typescript and we will see examples with an array of string and an array of objects.

Let’s first understand when use indexOf() or findIndex() method.

  • The indexOf() method is used for an array of primitive datatypes like string, number, etc or you can also say single dimension array.

  • The findIndex() method use when find index value from object element.

TypeScript indexOf() example

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.

1let exampleArrStr: Array<string> = [
2 'infinitbility',
3 'notebility',
4 'repairbility',
7let strIndex = exampleArrStr.indexOf('repairbility');
8console.log('exampleArrStr index', strIndex);


typescript, array, indexOf, example
TypeScript, array indexOf example.

TypeScript findIndex() example

The findIndex() method returns the index of the first element in the array that satisfies the provided testing function. Otherwise, it returns -1, indicating that no element passed the test.

1interface Users {
2 id: number;
3 name: string;
5let exampleArrObj: Array<Users> = [
6 { id: 1, name: 'infinitbility' },
7 { id: 2, name: 'notebility' },
8 { id: 3, name: 'repairbility' },
11let objIndex = exampleArrObj.findIndex((e: Users) => == 'notebility');
12console.log('exampleArrObj index', objIndex);


typescript, findIndex, example
TypeScript, findIndex example.

Thanks for reading…

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

melio affilitate

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide



More articles from Infinitbility

How to get array length in typescript?

TypeScript, get length of array example.

February 2nd, 2022 · 1 min read

How to add key and value from json array object in javascript

Javascript, add key-value pair in json object example.

February 1st, 2022 · 1 min read
© 2020–2022 Infinitbility
Link to $ to $ to $ to $ to $ to $mailto:[email protected]