Javascript. SQL-like Filtering

Here are some handy JavaScript functions to filter Arrays in SQL-like fashion, including selectOne and selectWhere methods.

Javascript. SQL-like Filtering

Here are some handy JavaScript functions to filter Arrays in SQL-like fashion

Array.prototype.selectOne = function (where) {
    var filtered = this.selectWhere(where);

    if (filtered.length > 0) {
        return filtered[0];
    }

    return null;
};

Array.prototype.selectWhere = function (where) {
    var columnName = where[0];
    var comparator = where[1];
    var compareValue = where[2];
    var filtered = [];

    for (var i = 0; i < this.length; i++) {
        var row = this[i];

        if (row[columnName] == compareValue) { // Leave as is, do not use "==="
            filtered.push(row);
        }
    }

    return filtered;
};
← Previous
Bootstrap 4 Resources
Next →
.NET. Entity Framework. Working with Database Transactions

Keep Reading

Explore more articles and insights

uid: SafeShortID and HexShortID — Time-Ordered IDs Without the Rude Words

uid: SafeShortID and HexShortID — Time-Ordered IDs Without the Rude Words

Read Article
Bootlegance: 70 Free Bootstrap 5 Themes with Native Dark Mode

Bootlegance: 70 Free Bootstrap 5 Themes with Native Dark Mode

Read Article
A Big, Opinionated List of PHP Libraries (2026 Edition)

A Big, Opinionated List of PHP Libraries (2026 Edition)

Read Article
View All Blog Posts