Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IE does not support Array includes or String includes methods

I have been working on a project and developing a JavaScript framework. The original code is about 700 lines so I only pasted this line. The includes method doesn't work on Internet Explorer. Is there any solution for this?

var row_cells = tbl_row.match(/<td[\s\S]*?<\/td>/g);      row.Cells = new Array();     if (onRowBindFuncText != null) { /*Fonksyon tanımlanmaışsa daha hızlı çalış*/          var cellCount = 0;         for (i = 0; i < row_cells.length; i++) {              var cell = new Cell();             $.each(this, function (k, v) {                  if ((row_cells[i]+"").includes("#Eval(" + k + ")")) {                      cell.Keys.push(new Key(k,v)); 

...Code goes on

like image 529
Onur Emrecan Özcan Avatar asked Jul 04 '15 13:07

Onur Emrecan Özcan


People also ask

Does IE support include?

includes was not supported in IE. I had to write substitute of this function. This function basically did 2 things: check if element is in array.

What can be used instead of includes in Javascript?

indexOf() The Array#indexOf() function is a common alternative to includes() . The indexOf() function returns the first index in the array at which it found valueToFind , or -1 otherwise.


2 Answers

Because it's not supported in IE, it is not supported also in Opera (see the compatibility table), but you can use the suggested polyfill:

Polyfill

This method has been added to the ECMAScript 2015 specification and may not be available in all JavaScript implementations yet. However, you can easily polyfill this method:

if (!String.prototype.includes) {   String.prototype.includes = function(search, start) {     'use strict';     if (typeof start !== 'number') {       start = 0;     }      if (start + search.length > this.length) {       return false;     } else {       return this.indexOf(search, start) !== -1;     }   }; } 
like image 162
alessandro Avatar answered Sep 22 '22 13:09

alessandro


@Infer-on shown great answer, but it has a problem in a specific situation. If you use for-in loop it will return includes "includes" function you added.

Here is another pollyfill.

if (!Array.prototype.includes) {   Object.defineProperty(Array.prototype, "includes", {     enumerable: false,     value: function(obj) {         var newArr = this.filter(function(el) {           return el == obj;         });         return newArr.length > 0;       }   }); } 
like image 34
Sunho Hong Avatar answered Sep 24 '22 13:09

Sunho Hong