-3

I have an array of strings (names) such as:

name =["John Doe","Lutfur Kabir", "Moshiur Imtiaz Rahman", "Clark Kent","Jenny Doe"]

I want to get the index/es of the name that has Doe in it. How do I go about doing it using JavaScript.

Mohammad Usman
  • 37,952
  • 20
  • 92
  • 95
Aadn
  • 93
  • 1
  • 5
  • The posted question does not appear to include [any attempt](https://idownvotedbecau.se/noattempt/) at all to solve the problem. StackOverflow expects you to [try to solve your own problem first](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users), as your attempts help us to better understand what you want. Please edit the question to show what you've tried, so as to illustrate a specific roadblock you're running into a [MCVE]. For more information, please see [ask] and take the [tour]. – CertainPerformance Dec 02 '18 at 05:55

2 Answers2

1

You can use Array.prototype.includes:

Find the previous answer here already

var categoriesPresent = ['word', 'word', 'specialword', 'word'];
var categoriesNotPresent = ['word', 'word', 'word'];

var foundPresent = categoriesPresent.includes('specialword');
var foundNotPresent = categoriesNotPresent.includes('specialword');

console.log(foundPresent, foundNotPresent); // true false
Arka Mallick
  • 1,206
  • 3
  • 15
  • 28
0

You can use .reduce() to create an array having indexes of strings containing the desired string:

let data =["John Doe","Lutfur Kabir", "Moshiur Imtiaz Rahman", "Clark Kent","Jenny Doe"];

let result = data.reduce((r, c, i) => {
  if(c.includes('Doe')) { r.push(i); }
  return r;
}, []);

console.log(result);
Mohammad Usman
  • 37,952
  • 20
  • 92
  • 95