1

I would like to check if a String exists in my array.

My Javascript Code :

if(Ressource.includes("Gold") === true )
         {
             alert('Gold is in my arrray');
         }

So Ressource is my array and this array contains :

Ressource ["Gold 780","Platin 500"] // I printed it to check if it was true

I don't understand why my test if(Ressource.includes("Gold") === true don't work.

Best regards, I hope someone knows what is wrong with this.

4
  • 4
    includes mathes the whole string not a part of it Commented Sep 15, 2018 at 16:11
  • hi @Adriani6 i already tryied this and it don't work to. Commented Sep 15, 2018 at 16:16
  • hello @ashishsingh there is no way to search only a world in my string ? Commented Sep 15, 2018 at 16:18
  • a direct method to achieve it , i am not aware. Indirectly, yes , you can refer the answers people have posted Commented Sep 15, 2018 at 16:20

4 Answers 4

5

The includes array method checks whether the string "Gold" is contained as an item in the array, not whether one of the array items contains the substring. You'd want to use some with the includes string method for that:

Ressources.some(res => res.includes("Gold"))
Sign up to request clarification or add additional context in comments.

5 Comments

@DorianGrn you mean like finding the string that includes Gold? That would be Array#find instead of Array#some.
@Bergi With your code how can I display the entire contents of the box of my table that contains the word gold.
@DorianGrn Which table? You seem to have an array of strings.
@Bergi i want to get the full string « Gold 780 » into a var ( the full sentence ) lf « Gold » exist, you gave me the way to check if the world gold exist but i don’t know how i can do this. I am new to js. And i meant array not table sorry.
@DorianGrn Like MinusFour suggested, use item = Ressource.find(res => res.includes("Gold")) which will return either the first array item that contains "Gold" or undefined if the condition is never satisfied
0

You should loop through your array until you find out if your value exist.

if (Ressource.some(x => x.includes("Gold") === true)) {
    alert('Gold is in my arrray');
}

1 Comment

With your code how can I display the entire contents of the box of my table that contains the word gold
0

Your problem is that you have a number along with Gold in the string in your array. Try using regex like this:

var Ressource = ["Gold 232331","Iron 123"]

if(checkForGold(Ressource) === true ) {
  console.log('Gold is in my array');
} else {
  console.log('Gold is not in my array');
}

function checkForGold(arr) {
   var regex = /Gold\s(\d+)/;
   return arr.some(x=>{if(x.match(regex))return true});
}  

The MDN docs have a excellent guide to regular expressions. Try this instead.

Comments

0

Another approach would be to use Array.prototype.find() and a simple RegExp. That would return the value of the element holding the search term. As said in most answers Array.prototype.includes() works if your search term matches exactly the array element Gold 780.

let Ressource = ["Gold 780","Platin 500"] ;
let found = Ressource.find(function(element) {
let re = new RegExp('Gold');
return element.match(re);
});
console.log(found);
// Working example of Array.prototype.includes()
if(Ressource.includes("Gold 780")) {
  console.log('Gold is in my arrray');
}

Working Fiddle

Comments

Your Answer

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