1

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

intercoder
  • 2,171
  • 7
  • 23
  • 34
  • you need to push the index inside the for loop not outside – Sharath Bangera Nov 15 '16 at 08:46
  • Sorry, it's not at all clear what you're trying to do here. Your `findIdx` function only accepts an array; which index is it supposed to find? Normally a function to find the index of a value in an array would need access to both the array *and* the value you wanted to find. – T.J. Crowder Nov 15 '16 at 08:47
  • you're only pushing one value in the indexes array, as sharath bangera suggested put your `indexes.push` inside the for loop and return `indexes` – Kevin Kloet Nov 15 '16 at 08:48
  • The various answers to this question may be of some use: http://stackoverflow.com/questions/9329446/for-each-over-an-array-in-javascript You don't want to use `for-in` for this without safeguards, your original `for` loop is the standard construct for this. – T.J. Crowder Nov 15 '16 at 08:49

4 Answers4

0

You should do something like this,

var findIdxWithForIn = function (arr) {
var indexes = [];
for (i in arr) {
var index = i;
indexes.push(index) 
console.log(index);
}
return indexes;
}

Now in case you want to find out what the indexes array contains then again you have put that in a for loop and extract

GraveyardQueen
  • 771
  • 1
  • 7
  • 17
0

indexes.push(index) Should be inside for loop. You were doing it wrong.

 var findIdxWithForIn = function (arr) {
      var indexes = [];
      for (i in arr) {
        var index = i; 
        indexes.push(index)
      }
      return indexes;
    }

    console.log(findIdxWithForIn(a));
0

Try this out

let a =  [1, 2, 3, 4, 5];


console.log('======= For Loop  ========')

/*
 For loop
*/
var findIdx = function (arr) {
var indexes = []; 
  for (let i = 0 ; i < arr.length; i++) {

    var index = arr[i];
    console.log(index);
    indexes.push(index);
  }
    return indexes
}

console.log(findIdx(a));

console.log('======= For In ========')

/*
 For In  Loop
*/

var findIdxWithForIn = function (arr) {
  var indexes = [];
  for (i in arr) {
    var index = i; 
    console.log(index);
    indexes.push(index);
  }
  return indexes //.push(index);
}

console.log(findIdxWithForIn(a));
spankajd
  • 934
  • 7
  • 13
0

You are printing var index which is not an index it is element in array:

var index = arr[i];
console.log(index);

You need to get index so var index = i;. Because i in for loop is actual index. You can also use arr.indexOf(...); to get index of specific element.

GROX13
  • 4,605
  • 4
  • 27
  • 41
  • thanks for clarifying that. Now bth functions are printing the index which is what I want, but they just print `0,1` instead of `0,1,2,3,4`. I edited the code – intercoder Nov 15 '16 at 08:59