Suggestions for Set

Rick Waldron waldron.rick at gmail.com
Mon Oct 1 17:48:50 PDT 2012


On Mon, Oct 1, 2012 at 8:40 PM, Nicholas C. Zakas <
standards at nczconsulting.com> wrote:

>  I've been playing around with the Set prototypes in Firefox and Chrome
> (knowing full well that they are incomplete and the spec hasn't been
> finalized yet), and I wanted to share a couple of learnings.
>
> First, since a Set can be initialized with an array, it stands to reason
> that you should be able to easily get an array back from the set. However,
> there is no way to do that right now short of using a loop and manually
> constructing an array. I would really like to see a *Set.prototype.toArray
> * method to easily change the Set back into an array. A simple use case
> would be de-duping an array:
>
> function dedupe(array) {
>     return (new Set(array)).toArray();
> }
>
>
>
In July, Array.from was upgraded to also accept objects with iterators (in
addition to Array-likes). Set is an iterator, so it could be used to
construct a new array:

function dedupe(array) {
    return Array.from(new Set(array));
}



Rick



>  Second, the lack of visibility into Sets is problematic. A very simple
> use case is when I want to see if the Set has been initialized yet but I
> can't know what keys might have been used at that point in time. Firefox
> implements a *Set.prototype.size* method which is useful to know what I'm
> dealing with. However, I'm not sure how frequently the number of items in
> the Set actually matters, so a *Set.prototype.isEmpty* would be equally
> useful for this case.
>


> Otherwise, I'm finding Sets very useful as a replacement for objects that
> I was using for the same purpose.
>
> Thanks,
> Nicholas
>
> --
> ___________________________
> Nicholas C. Zakashttp://www.nczonline.net
>
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20121001/5b59c774/attachment.html>


More information about the es-discuss mailing list