2

I am trying to search whether a string is present inside an array of strings using the string.includes() method as shown below

var theString = "clovers are the best";
var theArray = ["lovers", "loved", "clove", "love", "clovers"];
for (i = 0; i < theArray.length; i += 1) {
  if (theString.includes(theArray[i])) {
    console.log(theArray[i]);
  }
}

The output I get includes "lovers", "clove" and "love" in addition to the expected "clovers." How can I force the search to look for entire string only?

6
  • Use === instead of String.prototype.includes Commented Aug 2, 2016 at 2:49
  • 1
    Did you mean to write theArray[i].includes(theString)? Commented Aug 2, 2016 at 2:50
  • 1
    I think you want theArray.includes(theString) with no loop. Commented Aug 2, 2016 at 2:51
  • Ah, but in my actual code, "theString" is actually a sentence. For example theString = "clovers are the best" Commented Aug 2, 2016 at 2:52
  • 2
    Well how about you edit your question to give a complete example? Anyway, you can do what you want using a regular expression match instead of .includes(). Commented Aug 2, 2016 at 2:53

2 Answers 2

2

You're testing each element of the array to see if the element is present in the string. You can just test the array to see if a particular string is a member, which is closer to your description of the problem.

    var theString = "clovers";
    var theArray = ["lovers", "loved", "clove", "love", "clovers"];
    var idx = theArray.findIndex( e => e === theString );
    console.log(idx);
    // finding a string done two ways
    idx = theArray.indexOf( theString );
    console.log(idx);

If idx is not -1, the string is present in the array

Sign up to request clarification or add additional context in comments.

3 Comments

Is this different somehow from indexOf?
@torazaburo They do simliar things. indexOf takes a search term as an argument. findIndex takes a function. indexOf may be more appropriate in this instance since the function is a direct comparison
@Tibrogargan you should add this to the answer.
-1

It's returning the extras because they are a part of "clovers". My recommendation would be to split the string individual words:

var input = prompt("enter string").split(" ");
unallowed=",.()[]{}-_=+!@#$%^&*~`|\\<>?/\"':;"; //list of not allowed chars
for(i=0; i<input.length; i++)
{
    for(j=0;j<unallowed.length;j++)
    {
        input[i]=input[i].split(unallowed[j]);
        if(typeof(input[i])=="object")
            input[i]=input[i].join("");
    }
    
}
var theArray = ["lovers", "loved", "clove", "love", "clovers"];
for (i = 0; i < theArray.length; i += 1) {
  for(j=0; j<input.length; j++)
      if (input[j].toLowerCase()==theArray[i]) {
        console.log(theArray[i]);
  }
}
edit: fixed a few derps in my code.

2 Comments

What if the real sentence to be tested isn't hardcoded, and might contain punctuation? E.g., "I don't like clovers, but I like oak trees and tulips."
punctuation complicates it a bit, it just needs to be removed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.