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"]
annedroiid
  • 6,267
  • 11
  • 31
  • 54
  • 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()` – Yasin Yaqoobi Aug 08 '16 at 04:25

5 Answers5

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

Samuel Toh
  • 18,006
  • 3
  • 24
  • 39
  • I think he wants to find all elements that contains "component". – Yasin Yaqoobi Aug 08 '16 at 04:17
  • @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. – Samuel Toh Aug 08 '16 at 04:46
  • 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. – annedroiid Aug 09 '16 at 04:02
  • When I try to use this however it says that "Property 'find' does not exist on type 'string[]'". – annedroiid Aug 09 '16 at 04:03
  • @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? – Samuel Toh Aug 09 '16 at 04:36
  • I'm trying to do it in typescript as well, that's where the error came from. Sorry I should have mentioned that before. – annedroiid Aug 09 '16 at 04:39
  • @annedroiid not familiar with type script. Probably update your question with what you got right now and I'll try to see if I can debug it? – Samuel Toh Aug 09 '16 at 05:49
  • @annedroiid if you are trying to match exact string indexOf gives you the right index. What is the problem ? – Yasin Yaqoobi Aug 09 '16 at 14:58
2

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

James
  • 1,436
  • 1
  • 13
  • 25
1

Try using $.inArray() method.

var list=["component.part", "random-component", "prefix-component-name", "component"];
if($.inArray(" component",list) != -1){
    console.log("Item found");
}
jonju
  • 2,711
  • 1
  • 13
  • 19
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.

obataku
  • 29,212
  • 3
  • 44
  • 57
-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]);
      }
    }
Yasin Yaqoobi
  • 1,888
  • 3
  • 27
  • 38