How can I get the index of an array with the elements of the array | Sololearn: Learn to code for FREE!

+1

How can I get the index of an array with the elements of the array

5/25/2022 12:54:56 PM

Olufikayomi Jetawo

10 Answers

New Answer

+2

Did you mean to generate index or find index of a given value? if you want the index of a given value then <array>.indexOf() is your friend, not sure about index generation though ... https://www.w3schools.com/jsref/jsref_indexof_array.asp (Edited)

+1

It would be helpful if you gave an example input and output.

+1

if you are talking about javascript so try forEach, map or filter. example: var arr = ['javascript' , 'java' , 'python']; /*this will generate callback function the first parameter is return the foreach value second index and third array itself. */ arr.forEach(function(val,index,array){ if(val=="javascript"){ console.log(index); } }); and you can also try indexOf or search

0

Pardon??

0

I am trying to generate the index no of a particular element in an array

0

I know how to generate the element with the index no but I was thinking if it's possible to do the reverse

0

Do you mean finding an element in an array and trying to get the index? If that's the case you try the brute force route and do a for-loop and then print the index of where you found the element.

0

This is an example of what I mean if I have an array of Var fruits = ['apple','mango','orange']; I am trying to get the index of orange is there any method to use

0

Ipang gave you one. indexOf() is a method that you can use on the array and pass in orange or any value you want the index of.

0