猿问

js 判断汉字是否在范围内出现 如何写

provincelist(newVal){        var eidtCty = window.localStorage.citycode;        var newCty = JSON.stringify(eidtCty.substring(0,eidtCty.length-1));
        newVal.forEach((v,i)=>{          var forCty = JSON.stringify(v.provinceName)          if(forCty.indexOf(newCty) != -1){            console.log(123)            /* this.isdialog = true;
            this.alertarea() */
          }
        })
      }

forCty是
"北京"
"河北"
"上海"
"山东"
newCty是
"北京"

需求是 newCty里的城市 如果不在forCty范围内则执行操作 如果在范围内就不执行操作 这个应该咋判断 求助大佬


慕容3067478
浏览 563回答 2
2回答

阿晨1998

把“!= -1”改成“== -1”就可以了,如果没看错的话。if(forCty.indexOf(newCty) == -1){            console.log(123)            /* this.isdialog = true;             this.alertarea() */           }

慕后森

除了indexOf,也可以用inculdesif (forCty.includes(newCty)) {  // ...}
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答