2

Does anyone know of a way to check if a list contains a string without using indexOf? In my array some strings can contains parts of others, so indexOf will produce false positives.

As an example, how would I determine if "component" is in the array below?

["component.part", "random-component", "prefix-component-name", "component"]

Update:

It seems like my use of false positive was misleading. I meant that it would say that component was in there 4 times, when I want to match the string by itself.

ie. It should return false when checking for the presence of "component" in the below array.

["component.part", "random-component", "prefix-component-name"]
1
  • 1
    IndexOf won't give you false positive. It will give you 3. If you want to find all elements that has "otherstuff componenet" you can loop through your array and check with String.includes() Commented Aug 8, 2016 at 4:25

5 Answers 5

3

Use the Array.find API.

Example:

"use strict";

let items = ["component.part", "random-component", "prefix-component-name", "component"];

let found = items.find(item => { return item === "component.part" } );

if (found) {
    console.log("Item exists.");
}

For more usage example.

See: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/find

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

8 Comments

I think he wants to find all elements that contains "component".
@YasinYaqoobi the question is a little bit unclear and it does sound like the requirement was to search the error for items that have the word component in them. I have included another solution that do that. See below.
I apparently made myself unclear, I only wanted to match the exact string "component". The example was meant to show why I couldn't just use indexOf.
When I try to use this however it says that "Property 'find' does not exist on type 'string[]'".
@annedroiid the find api is for Javascript array only. String[] sounds java to me. Care to reveal your updated code in the question area so I can follow up?
|
2

One way is to use .find() to get the string you want from the Array.

Comments

1

Try using $.inArray() method.

var list=["component.part", "random-component", "prefix-component-name", "component"];
if($.inArray(" component",list) != -1){
    console.log("Item found");
}

Comments

0

Does anyone know of a way to check if a list contains a string without using indexOf? In my array some strings can contains parts of others, so indexOf will produce false positives.

false positives? Array.prototype.indexOf and Array.prototype.includes both use strict equality which makes that impossible here.

Comments

-1

IndexOf won't give you false positive. It will give you 3. If you want to find all elements that has "otherstuff componenet" you can loop through your array and check with String.includes()

Here is a beginner friendly solution.

    var arr = ["component.part", "random-component", 
    "prefix-component-name", "component", "asdf"]; 
    
    console.log(arr.indexOf('component')); // give u 3
    
    for (var i = 0; i < arr.length; i++){
      if (arr[i].includes('component')){
        console.log(arr[i]);
      }
    }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.