10 Answers
New Answer5/25/2022 12:54:56 PM
Olufikayomi Jetawo10 Answers
New AnswerDid 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)
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
I know how to generate the element with the index no but I was thinking if it's possible to do the reverse
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.
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
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.
Sololearn Inc.
535 Mission Street, Suite 1591Send us a message