I would like to know how to find the indexOf
in JS
between two array
, one array
contains the given values and the other array
the values that I want to know itsindexOf
var act = [10002197,10001755,10001087,10001879,3508477478,10001881];
var actselect = [10002197,10001755,10001087,10001881];
var posicion = act.indexOf(actselect); //en vez de un numero necesito insertar un array que lo haga por cada valor
console.log(posicion); //la respuesta debería se (0,1,5)
You already got an answer with
map
, and as your question saysforEach
, I'll give an example of how it would be withforEach
, taking an additional validation to know if the element exists or not because if not it would be filling thearray
with-1
which is the return value ofindexOf
when not the element exists.Edit
If you want to obtain the elements from a
array
given you can usemap
orfilter
to return thearray
according to the indices specified inactselect
You can use the method
map
of the objectArray
that allows you to treat the elements of an array one by one and returns an array with the results: