Search code examples
javascriptjqueryinternet-explorercross-browsercompatibility

IE Console Error: SCRIPT438: Object doesn't support property or method 'from'


I have a function on my site that takes a array of email strings and removes any duplicates. But it doesn't work on ie because of my use of the Array.from method. How can I convert to following code to work on ie?

let emailsListArray = ['reece@gmail.com', 'someone@gmail.com', 'another@gmail.com', 'reece@gmail.com', 'person@gmail.com', 'another@gmail.com'];


Array.prototype.unique = function() {
    return Array.from(new Set(this));
}

emailsListArray = emailsListArray.unique();

console.log(emailsListArray);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


Solution

  • also the Set methods dont have a full support in ie so u have 2 solution :

    1: just use the array methods compatible with all navigators : for exemple you can use this code :

    Array.prototype.unique = function () {
      return this.reduce(function (acc, el) {
        return acc.indexOf(el) === -1 ? acc.concat(el) : acc;
      }, []);
    };
    

    2: add polyfill file to project you can find exemple of array.from polyfill here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from