How to get the value found by the index?

Asked

Viewed 100 times

0

I have a code where I pass a value and I need to check if that value exists within the array, so I use the indexOf. What I would like is to take the value that the indexOf find, for example:

org = 3;
var array_t = [1,2,3,4,5];

receb = (array_t.indexOf(org));

In that case he will show that the 3 is in position 2. What I want is to take the amount 3. It is possible?

  • 1

    But you already have the 3, because you would want to catch the 3? '-'

  • Calm down. I’ve been redundant asking '-' I’ll redo myself here'

1 answer

5

Hmmm... the value that you pass to the method is the value that it thinks (if you think), so it already has the value you’re looking for right?

But it can always do so (although it is redundant):

org = 3;
var array_t = [1,2,3,4,5];
receb = array_t.indexOf(org);

var valorAchado = array_t[receb]; // dá 3

Browser other questions tagged

You are not signed in. Login or sign up in order to post.