".contains" does not work on Android phones, but works fine on a web page

I have a list of products and I want to copy the list of products by input parameter. so I used contains to scarching the input line in the product list. its working mode with a web page. but when I open the same page on a mobile web page, it does not work. and gives an error that "contains" does not determine.

 if(productlist[i].name.toLowerCase().contains(input_val.toLowerCase())) --my business logic-- 

after that I tried with indexOf , then its working in both cases.

 if(productlist[i].name.toLowerCase().indexOf(input_val.toLowerCase()) !== -1) --my business logic -- 

what's the problem for .contains?

+4
source share
1 answer

Use this Polyfill, (Ref: MDN )

 if(!('contains' in String.prototype)) { String.prototype.contains = function(str, startIndex) { return -1 !== String.prototype.indexOf.call(this, str, startIndex); }; } 

See Compatibility Chart in Can I Use ...

Update: You can also check this answer .

+2
source

Source: https://habr.com/ru/post/1492496/


All Articles