What do I mean with this? First let's look at some code I wrote:
let names = ['James', 'james', 'bob', 'JaMeS', 'Bob'];
let uNames = {};
names.forEach(n => {
let lower = n.toLowerCase();
if (!uNames[lower]) {
uNames[lower] = n;
}
});
names = Object.values(uNames);
console.log(names); // >>> (2) ["James", "bob"]
The goal here is to unique the given array case insensitive but keep one of the original inputs.
I was wondering if there is a more elegant/better performing solution to this problem than the one I came up with.
Just converting the whole array to lowercase before making it unique is not a solution, because I'd like the end result to consist only of values which were already in the input array. Which one (e.g. James
or james
or JaMeS
) is not relevant.