0

I have array = ["Michael","Abraham", "Gabriel","Festus", "Ahab"] how can I return elements in the array containing myValue = "ab" which should be (Abraham,Gabriel,Ahab), I understand "indexOf" checks for the complete match.

otejiri
  • 1,017
  • 6
  • 20

1 Answers1

1

You can accomplish this using the filter method of array, please reference the code snippet below:

const array = ["Michael","Abigail","Gabriel","Festus", "Ahab"]
console.log(array.filter((name) => name.indexOf("ab") > -1));
etarhan
  • 4,138
  • 2
  • 15
  • 29