how to use Indexof with array?
Loading
how to use Indexof with array?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Sachin SinghRank #66 · 26.2K reputationPosted May 11, 2022, 2:28 PM EDT
Muhammad Imran AnsariRank #326 · 7.6K reputationPosted May 11, 2022, 12:48 PM EDT
Gajendra JangidRank #157 · 14.9K reputationPosted May 11, 2022, 11:31 PM EDT
The indexOf() method returns the first index (position) of a specified value.The indexOf() method returns -1 if the value is not found.The indexOf() method starts at a specified index and searches from left to right.By default the search starts at the first element and ends at the last.Negative start values counts from the last element (but still searches from left to right).
Example:
const fruits = ["Banana", "Orange", "Apple", "Mango", "Apple"];
let index = fruits.indexOf("Apple", -1);
o abuzaid87 reputationPosted Mar 22, 2024, 3:34 PM EDT
thanks all for your help
omran abuzaid
Tuhin PaulRank #7 · 71.9K reputationPosted Mar 25, 2024, 6:55 AM EDT
Finding the index of a specific element:
const fruits = ["apple", "banana", "orange", "banana"]; const bananaIndex = fruits.indexOf("banana"); console.log(bananaIndex); // Output: 1 (index of the first "banana")Tuhin PaulRank #7 · 71.9K reputationPosted Mar 25, 2024, 6:56 AM EDT
Starting the search from a specific index:
const colors = ["red", "green", "blue", "yellow"]; const yellowIndex = colors.indexOf("yellow", 2); // Start search from index 2 console.log(yellowIndex); // Output: 3 (index of "yellow")Bhavesh Jha117 reputationPosted Apr 1, 2024, 3:54 AM EDT
Let us consider an array having name arr of string type.
string[] arr = new string[5]{"Bhavesh","Sachin","Sundar","Sparsh","Ritesh"}; Console.WriteLine(Array.IndexOf(arr,"Bhavesh")); //output:0 Console.WriteLine(Array.IndexOf(arr,"Sachin")); //output:1 Console.WriteLine(Array.IndexOf(arr,"Sundar")); //output:2 Console.WriteLine(Array.IndexOf(arr,"Sparsh")); //output:3 Console.WriteLine(Array.IndexOf(arr,"Ritesh")); //output:4