JavaScript in filter an associative array with another array


In this problem statement, our task is to filter an associative array with another array with the help of Javascript. So for doing this task we will use the filter method and includes the Javascript method.

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'
};

Understanding the problem statement

The problem statement is to write a function in Javascript by which we can simply filter an associative array with the help of another array. So we will have an associative array and filter criteria by which we will filter the given array.

Logic for the given problem

So we will use an algorithm to filter the elements of the array using another array. First we will use a filter method to create a new array with all the elements that pass the test implemented by the created function. So the test is whether the name property of the current object is included in the filter criteria array. Then we will use the includes method. This method will be used to check whether an array includes a certain value among its entries. So we will check that the name property of the current item is included in the filter criteria array.

Algorithm

Step 1 − Define a data object which contains name and age properties.

Step 2 − Declare a filter criteria which will work as another array to filter the given data array.

Step 3 − 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.

Step 4 − After filtering whole data we will console the filtered data.

Code for the algorithm

//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);

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.

Conclusion

In conclusion, filtering an associative array with another array in Javascript can be easily achieved using the filter and includes methods. The time and space complexity of this algorithm is reasonable for most use cases.

Updated on: 18-May-2023

615 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements