JavaScript - Find keys for the matched values as like query in SQL

In JavaScript, you can filter object properties based on value matching, similar to SQL's WHERE clause with LIKE operator. This is useful for searching through data structures and finding keys that match specific criteria.

Problem Statement

Given an object with key-value pairs, we need to find all keys whose values contain a specific search term (case-insensitive).

const obj = {
    "100": "Jaipur",
    "101": "Delhi", 
    "102": "Raipur",
    "104": "Goa"
};

We want to create a function that returns key-value pairs where the value includes our search query, just like SQL's WHERE value LIKE '%query%'.

Using forEach() Method

We can iterate through the object keys and build a result object containing only matching pairs:

const obj = {
    "100": "Jaipur",
    "101": "Delhi",
    "102": "Raipur", 
    "104": "Goa"
};

const findByQuery = (obj, query) => {
    const keys = Object.keys(obj);
    const res = {};
    
    keys.forEach(key => {
        // Case insensitive search
        if (obj[key].toLowerCase().includes(query.toLowerCase())) {
            res[key] = obj[key];
        }
    });
    
    return res;
};

console.log(findByQuery(obj, 'pur'));
console.log(findByQuery(obj, 'Del'));
{ '100': 'Jaipur', '102': 'Raipur' }
{ '101': 'Delhi' }

Using Object.entries() and filter()

A more functional approach using Object.entries() and filter():

const obj = {
    "100": "Jaipur",
    "101": "Delhi",
    "102": "Raipur",
    "104": "Goa"
};

const findByQueryFilter = (obj, query) => {
    return Object.fromEntries(
        Object.entries(obj).filter(([key, value]) =>
            value.toLowerCase().includes(query.toLowerCase())
        )
    );
};

console.log(findByQueryFilter(obj, 'ai'));
console.log(findByQueryFilter(obj, 'xyz')); // No matches
{ '100': 'Jaipur', '102': 'Raipur' }
{}

Advanced Example with Multiple Conditions

Enhanced version that supports multiple search terms:

const cities = {
    "100": "Jaipur Rajasthan",
    "101": "Delhi NCR", 
    "102": "Raipur Chhattisgarh",
    "103": "Mumbai Maharashtra",
    "104": "Goa India"
};

const findByMultipleQueries = (obj, queries) => {
    return Object.fromEntries(
        Object.entries(obj).filter(([key, value]) =>
            queries.some(query => 
                value.toLowerCase().includes(query.toLowerCase())
            )
        )
    );
};

console.log(findByMultipleQueries(cities, ['Delhi', 'Mumbai']));
console.log(findByMultipleQueries(cities, ['Rajasthan', 'India']));
{ '101': 'Delhi NCR', '103': 'Mumbai Maharashtra' }
{ '100': 'Jaipur Rajasthan', '104': 'Goa India' }

Performance Comparison

Method Readability Performance Use Case
forEach + manual build Good Fast Simple filtering
Object.entries + filter Excellent Moderate Functional programming style

Conclusion

Both approaches effectively filter object properties based on value matching. Use forEach() for better performance or Object.entries() with filter() for more readable, functional code. The case-insensitive search makes it similar to SQL's ILIKE operator.

Updated on: 2026-03-15T23:19:00+05:30

459 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements