I want to get get unique characters from some string using ES6's Set.
So let's assume that we have string var str = 'abcdeaabc'; and create set of characters from this string:
var str = 'abcdeaadbc';
var chars = new Set(str);
Now we have the set of unique characters: abcd.
I was surprised that char set has forEach method and has no map method.
Set is iterable object, so why can't we use map function to iterate over set?
I tried to pass chars set and chars.values() SetIterator to Array.prototype.map this way:
Array.prototype.map.call(chars, function(element) {...});
Array.prototype.map.call(chars.values(), function(element) {...});
But every try failed.
For example, let's assume that we want to get unique symbols from string and return an array of them with preceding underscore. eg.: ['_a', '_b', '_c', '_d']
Here are my two solutions:
// First:
var result = Array.from(chars).map(function(c) {
return '_' + c;
});
// Second:
var result = [];
chars.forEach(function(c) {
this.push('_' + c);
}, result);
But is there a way of invoking Array.prototype's map function with Set context? And if no - why?