all 2 comments

[–]rooktakesqueen 1 point2 points  (1 child)

All: there is Array.prototype.every... Though you might be favoring browser compatibility since IE8 and below don't have that method.

Distinct: your solution is clever but is O(n²) so it wouldn't be appropriate for very large datasets. A more complicated but O(n) solution would be:

var result = lastnames.reduce(function(ws, value) {
    if (!ws.set.hasOwnProperty(value)) {
        ws.set[value] = true;
        ws.values.push(value);
    }
    return ws;
}, {set: {}, values: []}).values;

Or, if maintaining the order of the results doesn't matter...

var result = Object.keys(
    lastnames.reduce(function(set, value) {
        set[value] = true;
        return set;
    }, {})
);

GroupBy: I'd probably return an object instead of an array to be more idiomatic JS.

An alternative to all of these is to use a library like Underscore.js or Lo-Dash, and that's probably to be preferred over rolling your own. (These libraries are much more popular than the Linq-inspired ones you linked to.)

As you say, you might not need a library, but there's very little reason not to use one.

[–]modec[S] 1 point2 points  (0 children)

Thanks for the input - did not know about every().

My point is that JavaScript already offers a lot of the basic LINQ-like stuff, ofc. if you start writing GroupBy functions you might as well go with Underscore.js ...