1

I've been looking around the website and all I find are questions regarding finding the index of a value in an array, but that value is the only occurrence of that value in the array. I was wondering is there was a way to find the index of a repeated value every time that it occurs.

Say there's and array like so:

var arr = [45,56,76,4,53,43,6,273,884,69,47,58,225,222,23,13,89,900,7,66,78,74,69];

Is it possible to loop through this and find the indexes of the value 69?

user2517142
  • 321
  • 1
  • 2
  • 5
  • 2
    Loops will be among the first things you'll learn in a basic programming tutorial. –  Jul 25 '13 at 01:47

6 Answers6

3

Here's a way to do it in modern browsers:

function findIndexes(arr, val){
  return arr.map(function(v,i){ return v==val && i }).filter(Number);
}

console.log(findIndexes(arr, 69)); //=> [9,22]
elclanrs
  • 92,861
  • 21
  • 134
  • 171
  • Or just `.filter()` instead of `.map()` ? ...oops, no that wouldn't work. –  Jul 25 '13 at 01:57
  • @CrazyTrain: How would you do that? You need to map the index in there somehow, `filter` expects you to return either `true/thy` or `false/y`. Am I missing something? – elclanrs Jul 25 '13 at 01:59
2

Of course it's possible. Computers are amazing.

var positions = [];
for (var i = 0; i < arr.length; ++i)
  if (arr[i] === 69)
    positions.push(i);

At the end of that loop, the array "positions" will contain all the indexes of the array where 69 was found.

You could generalize this:

function indexes(arr, value) {
  var rv = [];
  for (var i = 0; i < arr.length; ++i)
    if (arr[i] === value)
      rv.push(i);
  return rv;
}

and then:

var i69 = indexes(arr, 69);
Pointy
  • 405,095
  • 59
  • 585
  • 614
2

Since we're all posting various ways to perform simple tasks...

var arr = [45,56,76,4,53,43,6,273,884,69,47,58,225,222,23,13,89,900,7,66,78,74,69];

arr.reduce(function(res, n, i) {
    return n === 69 ? res.concat(i) : res;
}, []);
  • +1 for creativity. I was playing wit something similar but ended up posting the `map|filter` solution. – elclanrs Jul 25 '13 at 02:04
  • Only thing I don't like is the constant `.concat()`. Makes me feel dirty. Sure looks nicer than an `if` statement though. –  Jul 25 '13 at 02:06
  • You could probably make use of _reduce_, _charCodeAt_ and an _Array_ to do something neat in [this question](http://stackoverflow.com/questions/17845584/converting-a-random-string-into-a-hex-colour) too, which wouldn't be as crazy as my answer there. – Paul S. Jul 25 '13 at 02:06
1

Loop while the next indexOf is not -1..

function allIndicesOf(arr, val) {
    var found = [], i = -1;
    while (-1 !== (i = arr.indexOf(val, i + 1))) found.push(i);
    return found;
}

var arr = [0, 1, 2, 3, 2, 1, 8, 5, 2, 0, 4, 3, 3, 1];

allIndicesOf(arr, 3); // [3, 11, 12]
Paul S.
  • 64,864
  • 9
  • 122
  • 138
  • 1
    Ah, I was just about to post nearly the same example. Even the assignment in the `while` head. +1 –  Jul 25 '13 at 01:59
  • 1
    @CrazyTrain when I one line a `while` loop and it works as desired, I somehow feel like I've achieved something special. – Paul S. Jul 25 '13 at 02:02
0

My idea

function indexs(arr,val){
    var start = arr.indexOf(val),result = [];
    while(start >= 0){
        result.push(start);
        start = arr.indexOf(val,start+1);
    }
    return result;
}

Have fun :)

StupidDev
  • 342
  • 2
  • 12
0

var arr = [45,56,76,4,53,43,6,273,884,69,47,58,225,222,23,13,89,900,7,66,78,74,69],
    i = arr.length,
    o = {};

while( i-- ) {
    if( !o[ arr[i] ] ) {  
        `o[ arr[i] ] = [];`
    }
    o[ arr[i] ].push( i );
}

alert( o[69] );
Yuan Zhaohao
  • 574
  • 5
  • 4
  • To format your code, just select the entire code block, and click the **`{}`** button above the edit area. –  Jul 25 '13 at 02:40