The provided code defines a function `matchesCriteria` which checks if...

August 22, 2025 at 06:53 PM

const matchesCriteria = (rowData, filters) => filters.every(([key, value]) => rowData[key] === value);

The provided code defines a function matchesCriteria which checks if a data object (rowData) satisfies all the specified filtering criteria provided in the filters array. Here's a detailed breakdown:

Code Explanation:

  1. Input Parameters:

    • rowData: An object representing a row of data. It consists of key-value pairs where the key is a specific field/property, and the value is the corresponding data.
    • filters: An array of [key, value] pairs, where:
      • key is the name of a property in rowData.
      • value is the expected value for that property.
  2. Logic:

    • The function uses the .every() method on the filters array.
    • .every() tests if all elements of the array satisfy a given condition (i.e., the callback function returns true for every filter).
    • For each [key, value] pair in filters, it checks whether the value of the key in rowData is strictly equal (===) to the provided value.
  3. Return Value:

    • The function returns true if all conditions in the filters array match the corresponding values in the rowData object.
    • Returns false if at least one condition fails.

Example Usage:

const rowData = { name: 'Alice', age: 25, city: 'New York' };
const filters = [['name', 'Alice'], ['age', 25]];

const matchesCriteria = (rowData, filters) =>
  filters.every(([key, value]) => rowData[key] === value);

console.log(matchesCriteria(rowData, filters)); // true

const filters2 = [['name', 'Alice'], ['city', 'Los Angeles']];
console.log(matchesCriteria(rowData, filters2)); // false

What the Code Does:

The function matchesCriteria checks if all key-value pairs in the filters array match the corresponding key-value pairs in the rowData object. It performs a strict comparison (===) for this purpose. If they all match, it returns true; otherwise, it returns false.

Generate your own explanations
Download our vscode extension
Read other generated explanations

Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node