Created
November 11, 2020 21:27
-
-
Save DoctorDerek/2be918c3113f68ba96d6db553e400a36 to your computer and use it in GitHub Desktop.
ES5 Internet Explorer 9 version of Finding Unique Strings in JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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