0

I can't actually find a solution after browsing the internet and making some tests. I'm trying to find the indexes of where there are most 0s together. For example, this should return 3 and 6:

var arr1 = [1,0,1,0,0,1,0,0,1,1];
var joined1 = arr1.join(''); // "1010010011"
var ans = joined1.indexOf("00"); // returns 3 - want to return 3 & 6

And this should return 1 and 5:

var arr2 = [1,0,0,0,1,0,0,0,1,0,0,1,1];
var joined2 = arr2.join(''); // "10001000100111"
var ans2 = joined2.indexOf("000"); // returns 1 - want to return 1 & 5

The problem is that indexOf only returns the first index rather than both. How can I get it to return all instances where the condition is satisfied? thanks for any help

user8758206
  • 2,106
  • 4
  • 22
  • 45

3 Answers3

2

It appears that you're looking more at string operations and not array operations. In this case regex can help you. Specifically function arguments for String.prototype.replace(). Note that the code below does not escape special regular expression characters like .

function findAll(string, value) {
    var indices = [];
    string.replace(new RegExp(value, "g"), function (m, o) {
        indices.push(o);
    });
    return indices;
}
Aplet123
  • 33,825
  • 1
  • 29
  • 55
  • Interesting. What part does "g" play here? and the 'm' in the function? I'm trying to understand how this works! – user8758206 Jul 25 '18 at 15:38
  • The `g` stands for global and means to match every instance of the string. The `m` is the part that is matched but we don't need it. However, since it is the first argument we have to include it. – Aplet123 Jul 25 '18 at 15:59
1

This works. Search the string from reverse, store the index and get the substring on which it should be iterated again.

function findIndexes(arr, value)
{
    var str = arr.join(''), index = 0, result = [];
    while(index>-1)
    {
        index = str.lastIndexOf(value)
        index>-1 && (result.push(index), str = str.substring(0,index-1));
    }
    return result.reverse();
}

console.log(findIndexes([1,0,1,0,0,1,0,0,1,1],"00"));
console.log(findIndexes([1,0,0,0,1,0,0,0,1,0,0,1,1],"000"));
console.log(findIndexes([1,0,1,0,0,1,0,0,1,1],"0000"));
Vignesh Raja
  • 7,927
  • 1
  • 33
  • 42
0

Here a general solution to find the indexes of n consecutive matches, just first index of a consecutive match or all index of a consecutive match:

function findIndexOfElements(arr, search, times, allkeys) {
  var indexs = []
  let consecutives = 0;
  arr.map((o, i) => {
    if (o == search) { //<--we find what we are looking for
      if (allkeys) {
        indexs.push(i); //<--store all indexes
      } else if (consecutives == 0) {
        indexs.push(i); //<--store just first index
      }
      consecutives++;
    } else { //<--we don't find what we are looking for
      if (consecutives < times && consecutives > 0) {
        if (allkeys) {
          indexs.splice(-consecutives, consecutives); //<--remove all consecutives
        } else {
          indexs.splice(-1, 1); //<--remove just the last index
        }
      }
      consecutives = 0;
    }
  })
  return indexs;
}
var arr1 = [1, 0, 1, 0, 0, 1, 0, 0, 1, 1];
var arr2 = [1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1];
var arr3 = ["cat", "cat", "dog", "cat", "dog", "dog", "cat", "cat"];
//just index of first match
console.log(findIndexOfElements(arr1, 0, 2, false)) //[ 3, 6 ]
console.log(findIndexOfElements(arr2, 0, 3, false)) //[ 1, 5 ]
console.log(findIndexOfElements(arr3, "cat", 2, false)) //[ 0, 6 ]
//all indexes of match
console.log(findIndexOfElements(arr1, 0, 2, true)) //[ 3, 4, 6, 7 ]
console.log(findIndexOfElements(arr2, 0, 3, true)) //[ 1, 2, 3, 5, 6, 7 ]
console.log(findIndexOfElements(arr3, "cat", 2, true)) //[ 0, 1, 6, 7 ]
Emeeus
  • 5,072
  • 2
  • 25
  • 37