JSLinq Sum method

JSLINQ is an awesome library that attempts to mimic the functionality provided by .NET LINQ libraries in clientside javascript code.

However while it is a great implementation it lacks a Sum method so for anyone interested here is the Sum method for JSLINQ that I created…

Sum: function (clause) {
    var values = this.Select(clause).ToArray();

    if (values != null) {
        var result = 0;

        for (var i = 0; i < values.length; i++) {
            var val = values[i];
            if (!isNaN(parseFloat(val)) && isFinite(val)) {
                result = result + parseInt(val);
            }
        }

        return result;
    } else {
        throw new Error("Argument Null Source");
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s