56

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

ScottyG
  • 3,204
  • 3
  • 32
  • 42
Onur Emrecan Özcan
  • 642
  • 1
  • 6
  • 14

8 Answers8

75

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;
    }
  };
}
Bill
  • 4,506
  • 2
  • 18
  • 29
ale
  • 10,012
  • 5
  • 40
  • 49
  • Thanks @InferOn. This is great stuff. I'm picking up JavaScript right now, but wanted to ask what exactly the `arguments` variable being passed into `apply()` is? I `console.log()` it in IE and see that it is some sort of `Argument` object, but I don't understand where it's coming from exactly since it doesn't appear to be defined anywhere. – Yu Chen Oct 03 '17 at 16:23
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments could help you – ale Oct 03 '17 at 17:10
50

@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;
      }
  });
}
Sunho Hong
  • 527
  • 4
  • 3
11

You could just use .search() > -1 which behaves in the exact same way. http://www.w3schools.com/jsref/jsref_search.asp

if ((row_cells[i]+"").search("#Eval(" + k + ")") > -1) {
Patrick Duncan
  • 549
  • 1
  • 6
  • 17
7

This selected answer is for String, if you are looking for 'includes' on an array, I resolved my issue in an Angular project by adding the following to my polyfills.ts file:

import 'core-js/es7/array';
patrickbadley
  • 2,510
  • 2
  • 29
  • 30
  • What's up with `polyfills.ts`? Why do you have that file in your project? – KimchiMan Jun 13 '21 at 05:31
  • 1
    Good question, I just realized that this question was not specific to Angular when I posted my answer so my answer could be confusing (I updated it to specify that it pertains to Angular). polyfills.ts comes out of the box with a new angular (8/9/10...) project. – patrickbadley Jun 14 '21 at 01:12
4

This is a polyfill for TypeScript projects, taken from https://developer.mozilla.org/nl/docs/Web/JavaScript/Reference/Global_Objects/Array/includes and modified to be valid TypeScript:

if (!Array.prototype.includes) {
    Object.defineProperty(Array.prototype, 'includes', {
        value: function(searchElement, fromIndex) {

            if (this == null) {
                throw new TypeError('"this" is null or not defined');
            }

            const o = Object(this);
            // tslint:disable-next-line:no-bitwise
            const len = o.length >>> 0;

            if (len === 0) {
                return false;
            }
            // tslint:disable-next-line:no-bitwise
            const n = fromIndex | 0;
            let k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);

            while (k < len) {
                if (o[k] === searchElement) {
                    return true;
                }
                k++;
            }
            return false;
        }
    });
}
mvermand
  • 5,829
  • 7
  • 48
  • 74
1
if (fullString.indexOf("partString") >= 0) {
//true 

} else {
//false
}
0
var includes = function(val, str) {
  return str.indexOf(val) >= 0;
};
mohan mu
  • 73
  • 1
  • 4
0

jquery got a solution for that:

if ($.inArray(val,ar)===-1){
    console.log ("val not found in ar");
}
else{
    console.log ("val found in ar");
}

the $.inArray(val,ar,[startingIndex]) function.

shayuna
  • 476
  • 4
  • 8