Skip to content

Instantly share code, notes, and snippets.

@DoctorDerek
Created November 11, 2020 21:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DoctorDerek/2be918c3113f68ba96d6db553e400a36 to your computer and use it in GitHub Desktop.
Save DoctorDerek/2be918c3113f68ba96d6db553e400a36 to your computer and use it in GitHub Desktop.
ES5 Internet Explorer 9 version of Finding Unique Strings in JavaScript
var stringsES5 = ["👌", "👌", "👌", "🐱‍👤", "🐱‍👤", "🐱‍🚀", "🐱‍🚀"]
var uniqueStringsES5 = []
var stringObjectES5 = {}
// Since JavaScript objects must have unique properties
// (keys), you can find unique strings using an object.
for (var i = 0; i < stringsES5.length; i++) {
var string = stringsES5[i]
// Object keys that haven't been set are undefined,
// which is one of JavaScript's falsy values.
if (!myStringObject[string]) {
myUniqueStrings.push(string)
myStringObject[string] = true
}
}
// In ES5, you can't use Array.from(), nor the ... spread
// operator, so you must .push() the strings to a new array.
console.log(myUniqueStrings)
// Output: Array(3) [ "👌", "🐱‍👤", "🐱‍🚀" ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment