-2

For example, I have a word such as "Unfortunate" and I need to locate all the "t"s inside the string.

indexOf doesn't work here because it only locates the first sub-string it finds and nor do all the javascript methods I could find

TNEG
  • 1
  • 1

2 Answers2

1

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

var indexes = getAllIndexes("Unfortunate", "t");
console.log(indexes);
Weedoze
  • 13,683
  • 1
  • 33
  • 63
0

var indexArr = [];
var testSrt = "Unfortunate".split("");
testSrt.forEach(function(element, index) {
   if (element == "t") {
      indexArr.push(index);
   }
});
console.log(indexArr);
ozil
  • 6,930
  • 9
  • 33
  • 56
  • We should not answer very obvious question. Instead, we should check and if possible mark them as duplicate – Rajesh Oct 21 '16 at 13:34