js从数组中查找是否包含对象 js中判断数组中是否含有某个字符串方法?

js中判断数组中是否含有某个字符串方法?如果(!([]中的“includes”){Array.prototype.includes包括=函数(值){var self=this for(var i=0

js中判断数组中是否含有某个字符串方法?

如果(!([]中的“includes”){Array.prototype.includes包括=函数(值){var self=this for(var i=0 i<这个。长度i){if(self[i]==value){return true}else{return false}}}[1,2,3,4,65,67,89,4].includes(67)//真[1,2,3, 4, 65, 67, 89, 4]. Includes(88)//false

JavaScript中没有对应的数组对象方法。您可以使用下面的自定义方法来尝试函数array包含(a,b){//判断array a是否包含bfor(vari=0I)<b.lengthi公司){t=falsefor(varj=0j&长度){if(B[i]==a[J]){t=true break}}if(!T) 返回值}

可以通过indexof找到,否则返回-1。

代码示例:

let arr=[1,2,undefined,“listening to the wind is the wind”,“echo”

//use indexof to find the subscript feature

let result=排列索引(“听风就是风”//3

if(result>-1){

//做点什么…]}