Hướng dẫn not includes javascript

Asked 6 years, 7 months ago

Viewed 116k times

I have this javascript code:

if (fromState.name == "home.subjects.subject.exams.exam.tests.test" &&
    toState.name == "home.subjects.subject.exams.exam.tests") {
       tes.test.current = false; 
       tes.test = null;               
}

I understand that I can do a simple match here:

toState.name == "home.subjects.subject.exams.exam.tests"

To check the toState name.

But how could I check the toState.name does not include the string:

"home.subjects.subject.exams.exam.tests" ?

for example the toStateName could be:

"home" or "home.access" or "home.city"

asked Feb 12, 2016 at 6:07

1

ES6 version of this is (check out answer from Allison):

!str1.includes(str2)

The original accepted answer was:

You are looking for indexOf

var x = "home.subjects.subject.exams.exam.tests";
console.log(x.indexOf('subjects'));     // Prints 5
console.log(x.indexOf('state'));        // Prints -1

answered Feb 12, 2016 at 6:10

AnanthAnanth

4,0722 gold badges19 silver badges26 bronze badges

answered Sep 29, 2018 at 1:24

Hướng dẫn not includes javascript

AllisonAllison

1,5971 gold badge17 silver badges23 bronze badges

1

var include_flag = toState.name.includes("home.subjects.subject.exams.exam.tests");
return !include_flag;

Using the JS includes method would probably be your best bet. I get I'm a little late in answering this question, but I was just googling this myself and came up with this answer after some fiddling with the code. This code will return true if toState.name does NOT include the string given.

Hope this helps anyone searching the same question I had!

answered Aug 15, 2018 at 14:00

Hướng dẫn not includes javascript

briflan26briflan26

1011 silver badge6 bronze badges

But how could I check the toState.name does not include the string:

var strArr = "home.subjects.subject.exams.exam.tests.test".split(".");
var name = "home.subjects.subject.exams.exam.tests";
var contains = false;

strArr.reduce( function(prev, current){

   if ( name.indexOf( current ) != -1 )
   {
      contains = true;
   } 

   return prev + "." + current;

} );

if (contains)
{
   alert( "yes it contains" );
}
else
{
   alert( "Nope" );
}

answered Feb 12, 2016 at 6:11

gurvinder372gurvinder372

65.1k9 gold badges69 silver badges90 bronze badges

2

The original question contained a "?" ternary operator, so here is how to do it using a ternary operator.

Suppose you're writing a poker game and have a rainbow flop. Now suppose you want the missing suit from the flop.

let flop = "3h Js 9c";
let missingSuit = !flop.includes("c") ? "c" : 
                  !flop.includes("d") ? "d" : 
                  !flop.includes("h") ? "h" : "s";
// missingSuit equals "d"

answered Feb 12, 2019 at 3:44

2

For me jQuery Version is 1.10.2

To check the substring within the string you can use includes jQuery function like this

var sub_str = "find"
var main_str = "find me if you can"
result = main_str.includes(sub_str)
if result{
  * Stuff todo if result found
}else{
  * Stuff todo if result not found
}

result true; // if you have sub string finder method separately and calling it from somewhere else.

answered Jun 17, 2019 at 8:50

Hướng dẫn not includes javascript