0

I want to get the position of "they" and "some" for all occurrences of it in the array split_Sentence by using a for loop function. Then I want to build an array from the output of the for loop function. But the problem is that whenever I use indexOf() to find the position it always gives the position of the first occurence. I don't know how to create an array from the output of a for loop.
can anybody help me out I would be grateful.

var Sentence = "some warriors know when they are defeated, whether some of them are too stubborn to accept it and they are the one who die. "
var split_Sentence = Sentence.split(" ")
/* function() ==> then sort an array listing all its output*/
Expected outcome: final array should be like this array= (0,4,8,18) 

Please do ask me if you didn't understand my question

Souradip Mandal
  • 35
  • 3
  • 11

1 Answers1

2

Note: do handle upper case lower case things as needed.

const Sentence = "some warriors know when they are defeated, whether some of them are too stubborn to accept it and they are the one who die.";
const arrayOfIndex = Sentence
  .split(" ")
  .map((word, index) => word === "some" || word === "they" ? index : false)
  .filter(x => x !== false);
console.log({arrayOfIndex});
Harsh Gundecha
  • 1,139
  • 9
  • 16