0

I am struggling with doing an array search that includes a piece of text that must include back slashes in it. I have tried includes(''); negating includes(''); and also trying similar using indexOf('').

I have a simple array, with at maximum four values; typically it has two, here is how it typically looks:

{tks: Array(2)}
tks: Array(2)
0: "https://mycoolwebsite.net/arcgis/rest/services"
1: "https://mycoolwebsite.com/arcgis/rest/services"
length: 2
__proto__: Array(0)
__proto__: Object

Here are the simple checks I'm trying to do: My second check with *includes* 'wdt' text seems to be working so I assume it's something with the backslashes. Anyway I can handle this? I'm perplexed why my if and else both get hit with the first check below using back slashes... I added the negating on the else to double check.. with and without that in the else, else is always hit.

    // right before doing the search I am building the array, just to add more context

    for (let i = 0; i < coolObj.length; i++) {
      if (coolObj[i].url){
          tr = coolObj[i].url;
          tks.push(tr);

          console.log({tks});     
       }
     }

    console.log({tks}); // consoles as I have included above ^

    if (tks.includes('/arcgis/rest/services')) {
            
      console.log({tks});     

    } else if (!tks.includes('/arcgis/rest/services')) { 

      console.log({tks});

      console.log('does not include correct url');
      aT = '/arcgis/rest/services';
      lP = false;
      filterAt(aT);
    }
    if (tks.includes('wdt')) {
      console.log({tks});

    }else {
      console.log({tks});
      wT = 'wdt';
      filterWt(wT);
    }
3
  • 2
    You only doing a compare of the whole value,.. eg. None of you compare have the full url with https://... etc in them, IOW: none of your includes are going to return true. Commented Sep 30, 2020 at 19:24
  • Wow... then what could I do to achieve 'contains' specific text? Commented Sep 30, 2020 at 19:26
  • 1
    You will want to loop the array, and check each array item. Commented Sep 30, 2020 at 19:32

1 Answer 1

1

From the MDN docs: The includes() method determines whether an array includes a certain value among its entries, returning true or false as appropriate.

You have to test the strings using String.prototype.includes within the array elements thus:

const arr = ["https://mycoolwebsite.net/arcgis/rest/services", "https://mycoolwebsite.com/arcgis/rest/services"];

arr.forEach(el => {
  if (el.includes('/arcgis/rest/services')) {
    console.log(el, ': includes url');
  }
  else {
    console.log('does not include correct url');
  }
  if (el.includes('wdt')) {
    console.log(el, ': includes wdt');
  } else {
    console.log('does not include correct wdt');

  }

});

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

Comments

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.