1

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>
Reece
  • 2,581
  • 10
  • 42
  • 90
  • 1
    IE has an ES5 JS engine, so there's nothing even near equal to Set. You've to manually remove the duplicates from the array. – Teemu Oct 30 '20 at 15:48
  • @Teemu my JS is inside a script tag within the PHP page. I don't think your solution would work for me would it? – Reece Oct 30 '20 at 15:52
  • 1
    Maybe my comment was a bit ambiguous, with "manually" I mean [something like this](https://stackoverflow.com/questions/1960473/get-all-unique-values-in-a-javascript-array-remove-duplicates), opposed to creating a `Set`, which removes the duplicates automatically. – Teemu Oct 30 '20 at 16:02
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from#Polyfill – epascarello Oct 30 '20 at 16:32
  • You can refer to the first method of nafaa's answer. I have tested and it works in IE 11. The original answer uses arrow functions which is not supported in IE and I convert it to ES5 syntax then it is compatible with IE 11. – Yu Zhou Nov 02 '20 at 05:54

1 Answers1

2

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

Yu Zhou
  • 11,532
  • 1
  • 8
  • 22