
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Filter Associative Array Using Another Array in JavaScript
In this problem statement, our task is to write a JavaScript program by which we can simply filter an associative array with the help of another array. Before discussing the solution for the given problem, let's familiarize ourselves with associative array first.
What is an associative array?
An associative array is a data structure which is used to store the key and value pairs. In this each key is associated with a value given to the key. The keys in this array are unique identifiers that can be used to retrieve their respective values. In an associative array the keys do not have numeric indexes. The keys can be any string or numerical values. These kinds of arrays can be used for situations where we need to access data based on non numeric keys.
In JavaScript, objects are a kind of associative array because they store key value pairs, in which the keys can be any string or symbol. Let's see an example:
const customer = { name: 'Rishi', age: 25, city: 'Mumbai' };
Using filter() and inlcudes() Method
In this approach, declare a filter criteria which will work as another array to filter the given data array. With the help of the filter() method we will filter the item of the given array which includes the above criteria array names in it. And store the result in a filtered array.
Example
Let's see the practical implementation of the above approach ?
//define data with name and age const data = [ { name: 'Anita', age: 45 }, { name: 'Tanvi', age: 20 }, { name: 'Bittu', age: 32 } ]; //filter data on certain criteria const filterCriteria = ['Anita', 'Bittu']; const filteredData = data.filter(item => filterCriteria.includes(item.name)); console.log(filteredData);
Output of the above code is ?
[ { name: 'Anita', age: 45 }, { name: 'Bittu', age: 32 } ]
Complexity
The time taken by the above method is O(n) where n is the number of objects in the data array. And the space complexity is also O(n), because we have created a new array to store the filtered objects.
Using filter() and some() Method
This is another way to filter an associative array with another array. Here, the filter() method creates a new array with all elements that pass the test implemented by the provided function. Then, some() method tests whether at least one element in the array passes the test implemented by the provided function.
Example
In this example, we are using filter() and some() method to filter an associative array with another array in JavaScript.
// define an associative array const data = [ { name: 'Anita', age: 45 }, { name: 'Tanvi', age: 20 }, { name: 'Bittu', age: 32 } ]; // define the array to filter with const filterCriteria = [20]; // custom filter function function filterWithArray(item, filter) { return filter.some(filterItem => filterItem === item.age); } // filter the associative array const filteredData = data.filter(item => filterWithArray(item, filterCriteria)); console.log(filteredData);
On running the above code, following output will be displayed ?
[ { name: 'Tanvi', age: 20 } ]
Complexity
The time complexity of above code is O(n * m) where n is the number of elements in data array and m is the number of elements in filterCriteria array. The auxiliary space complexity is O(1).