user8758206
user8758206

Reputation: 2191

Find all instances of array where condition is met, rather than just the first

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

Upvotes: 0

Views: 101

Answers (3)

Emeeus
Emeeus

Reputation: 5250

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 ]

Upvotes: 0

Vignesh Raja
Vignesh Raja

Reputation: 8761

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"));

Upvotes: 1

Aplet123
Aplet123

Reputation: 35540

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;
}

Upvotes: 2

Related Questions