Douglas Crockford

Blog

Books

Videos

2024 Appearances

Slides

JavaScript

Misty

JSLint

JSON

Github

Electric Communities

Flickr Photo Album

LinkedIn

Mastodon/Layer8

ResearchGate

Pronouns: pe/per

About

Set Practice

Operate on arrays as though they are enumerable sets.

function objectify(array) {

Make an object in which the keys are the elements of an array.

    const objectification = Object.create(null);
    array.forEach(function (element) {
        objectification[element] = true;
    });
    return objectification;
}

export default Object.freeze({

    union: function (the_zeroth_set, the_wunth_set) {

Make the union of two arrays of strings.

        return Object.keys(
            Object.assign(
                objectify(the_zeroth_set),
                objectify(the_wunth_set)
            )
        );
    },

    intersection: function (the_zeroth_set, the_wunth_set) {

Make the intersection of two arrays of strings.

        const the_objectified_wunth = objectify(the_wunth_set);
        return the_zeroth_set.filter(
            function (element) {
                return the_objectified_wunth[element] !== undefined;
            }
        );
    },

    difference: function (the_zeroth_set, the_wunth_set) {

Make the difference of two arrays of strings.

        const the_objectified_wunth = objectify(the_wunth_set);
        return the_zeroth_set.filter(
            function (element) {
                return the_objectified_wunth[element] === undefined;
            }
        );
    }

});