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
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
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);
var indexArr = [];
var testSrt = "Unfortunate".split("");
testSrt.forEach(function(element, index) {
if (element == "t") {
indexArr.push(index);
}
});
console.log(indexArr);