JS中List.contains() 替代方法 List.indexOf(“str“) != -1 

tech2025-10-11  4

JS如何判断字符串在List中是否存在:

String类型有一个方法:contains(),该方法是判断字符串中是否有子字符串。如果有则返回true,如果没有则返回false。

但是Js没有constains()方法,经查找发现可以使用     List.indexOf("str") != -1   代替;

indexOf();   方法的作用是:返回字符串中指定字符串值的第一个匹配项。(注意:indexOf()方法是区分大小写的!)

如果要检索的字符串值没有出现,则该方法返回-1。

即是:List.indexOf("str") != -1    ==>true    ==>表示List中存在该字符串;

var empNo = row[0].empNo; var sendEmpNo = '[[${empNoList}]]';//获取后端传过来的ModelMap,其中empNoList是一个List if (sendEmpNo.indexOf(empNo) != -1){ $.modal.alertError("该人员已存在,不能重复派单"); } else { dispatch(); }

 

最新回复(0)