javascriptinternet-explorerecmascript-6datatablesspread-syntax

How to achieve the spread operator in IE 11?


For some "extraction" operation I am trying to get the following snippet work, without using the spread operator, because IE 11 doesn't knows about spread operators.

works in Chrome, but not IE 11:

html_col_width =[{"targets":0, "width":50}, {"targets":1, "width":100},{"targets":2, "width":442}]

        ... some other code
        order: [
            [response.order_by_column, response.order_by]
        ],
        columnDefs: [
                      ...html_col_width,
                      {other: stuff},
                      {other: stuff}
    })

See columnDefs: ...html_col_width

How can I achieve the following without the spread operator:

columnDefs: [
          {"targets":0, "width":50},
          {"targets":1, "width":100},
          {"targets":2, "width":442},
          {other: stuff},
          {other: stuff}
    })

I have read and tried the following, but this is not working if the array of objects contains 2 keys: Spread Operator equivalent in IE - Javascript. The content at the provided link is about merging different objects, which makes the question rather different.


Solution

  • What you are using is called "Array Destructuring": https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment

    As you said, it doesn´t work in IE, and that is because it´s part of ES6 and that is not (and never will) supported in IE 11.

    One option to solve it is using transpilers like Babel.

    Other option, is to define your own function:

    var __spreadArrays = (this && this.__spreadArrays) || function () {
    for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
    for (var r = Array(s), k = 0, i = 0; i < il; i++)
        for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
            r[k] = a[j];
    return r;
    
    };
    
    var html_col_width = [{ "targets": 0, "width": 50 }, { "targets": 1, "width": 100 }, { "targets": 2, "width": 442 }];
    var columnDefs = __spreadArrays(html_col_width, [{ other: 'stuff' }, { other: 'stuff' }]);