Search from an array of objects via array of string to get array of objects in JavaScript

Filtering an array of objects based on keys from another array is a common requirement in JavaScript. This tutorial shows how to extract matching objects using various approaches.

Problem Statement

Given an array of strings and an array of objects, we need to filter objects whose KEY property exists in the string array:

const searchKeys = ['1956888670', '2109171907', '298845084'];
const users = [
    { KEY: '1262875245', VALUE: 'Vijay Kumar Verma' },
    { KEY: '1956888670', VALUE: 'Sivakesava Nallam' },
    { KEY: '2109171907', VALUE: 'udm analyst' },
    { KEY: '298845084', VALUE: 'Mukesh Nagora' },
    { KEY: '2007285563', VALUE: 'Yang Liu' },
    { KEY: '1976156380', VALUE: 'Imtiaz Zafar' },
];

console.log('Search keys:', searchKeys);
console.log('Total users:', users.length);
Search keys: [ '1956888670', '2109171907', '298845084' ]
Total users: 6

Using Array.filter() with indexOf()

The filter() method combined with indexOf() provides a straightforward solution:

const filterByKey = (searchKeys = [], objects = []) => {
    return objects.filter(obj => {
        const { KEY } = obj;
        return searchKeys.indexOf(KEY) !== -1;
    });
};

const result = filterByKey(searchKeys, users);
console.log('Filtered users:', result);
Filtered users: [
  { KEY: '1956888670', VALUE: 'Sivakesava Nallam' },
  { KEY: '2109171907', VALUE: 'udm analyst' },
  { KEY: '298845084', VALUE: 'Mukesh Nagora' }
]

Using Array.filter() with includes()

The includes() method offers more readable code than indexOf():

const filterWithIncludes = (searchKeys, objects) => {
    return objects.filter(obj => searchKeys.includes(obj.KEY));
};

const result2 = filterWithIncludes(searchKeys, users);
console.log('Using includes():', result2.length, 'matches found');
Using includes(): 3 matches found

Using Set for Better Performance

For larger datasets, converting the search array to a Set improves lookup performance:

const filterWithSet = (searchKeys, objects) => {
    const keySet = new Set(searchKeys);
    return objects.filter(obj => keySet.has(obj.KEY));
};

const result3 = filterWithSet(searchKeys, users);
console.log('Using Set:', result3);
Using Set: [
  { KEY: '1956888670', VALUE: 'Sivakesava Nallam' },
  { KEY: '2109171907', VALUE: 'udm analyst' },
  { KEY: '298845084', VALUE: 'Mukesh Nagora' }
]

Performance Comparison

Method Time Complexity Best For
indexOf() O(n×m) Small arrays
includes() O(n×m) Small arrays, readable code
Set.has() O(n+m) Large datasets

Complete Example with Validation

const searchAndFilter = (searchKeys, objects, keyProperty = 'KEY') => {
    if (!Array.isArray(searchKeys) || !Array.isArray(objects)) {
        throw new Error('Both parameters must be arrays');
    }
    
    const keySet = new Set(searchKeys);
    return objects.filter(obj => keySet.has(obj[keyProperty]));
};

try {
    const filtered = searchAndFilter(searchKeys, users);
    console.log(`Found ${filtered.length} matching objects`);
    filtered.forEach(user => console.log(`${user.KEY}: ${user.VALUE}`));
} catch (error) {
    console.error('Error:', error.message);
}
Found 3 matching objects
1956888670: Sivakesava Nallam
2109171907: udm analyst
298845084: Mukesh Nagora

Conclusion

Use includes() for small datasets and readable code, or Set.has() for better performance with large arrays. The filter() method provides an elegant solution for extracting matching objects based on key arrays.

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

405 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements