I'm trying to print the indexes of values of an array. When I do this:
let a = [1,2,3,4,5];
var findIdx = function (arr) {
for (let i = 0 ; i < arr.length; i++) {
var indexes = [];
var index = arr[i];
console.log(index);
return indexes.push(index);
}
return indexes;
}
console.log(findIdx(a));
// => 0,1 // return
Which is not what I want, I would like to get the index of those values. Furthermore, my return
is returning 1
. I would like an array made of those indexes. What am I missing?
When I do a for in
loop I get what I want on my console, but I'm still returning the value where that index is found.
let a = [1,2,3,4,5];
var findIdxWithForIn = function (arr) {
var indexes = [];
for (i in arr) {
var index = i;
console.log(index);
return indexes.push(index);
}
return indexes
}
console.log(findIdxWithForIn(a));
// => 0, 1 // return
TL;DR: Here is a jsfiddle with the above code.
Note that I would like just to all the indexes inside that array