'JavaScript filter array of objects on multiple values dynamically

Hi Everyone i wanted to filter an array based on the array that I receive which contains column name condition and the value so let me just how the two array looks. So this is what the array looks like from which I want to filter

[
            {
                "unique_id": 35264,
                "flash_candidate_id": 0,
                "first_name": "YAP  CHIN SIEN",
                "last_name": "",
                "email_id": "[email protected]",
                "tag_name": "Career Page ( sign up )",
                "position_name": "Fraud & Risk Specialist ",
                "campaign": "Airbnb Ireland (ABNB-MYS)",
                "location": "Malaysia",
                "job_req_id": 22017,
                "test_name": "Personality Test",
                "test_question": "AirBnB - Chinese Email Exam",
                "overall_score_average": null,
                "overall_score_percentage": "0%",
                "question": "",
                "answer": "",
                "question_score": 0,
                "question_score_percentage": "0%",
                "score": "",
                "grade": "",
                "test_comments": "",
                "presonality_result": "The Dynamic Thinker - ENTJ",
                "status": "Open",
                "remarks": "",
                "source": "",
                "medium": ""
            },
]

This is the second filter array which contains the column name, value, and should it contain or not

filterArray: [
    {
        "columnName": "first_name",
        "condition": "equal to",
        "value": "Some name"
    },
     {
        "columnName": "last_name",
        "condition": "equal to",
        "value": "Some last name"
     },
]

So I don't understand how I can filter the original array based on the filter array that I receive and the thing is the objects props will be dynamic so if one time first_name will be there if they are doing for selected data and the other time the original array will contain totally different values based on the selected data that's why I am asking dynamically.

This is what i had tried but the problem is it will do for only at a sinle time but i wanna do for multiple values and props

function getFilteredCodes(array, key, value) {
    return array.filter(function (o) {
        return o[key] === value;
    });
}
filteredCodes = getFilteredCodes(array, "first_name", "test user");


Solution 1:[1]

Seems like quite a complex case. Not really certain if I understood what you want to achieve here and it's also not clear whether you want to have logical AND or OR. Anyway, this could be a solution:

const compEquals = (a, b) => a === b;
const compGt = (a, b) => a > b;
const compLt = (a, b) => a < b;

const filterRules = [
    {
        propName: 'first_name',
        comparator: compEquals,
        refValue: 'Some name',
    },
    {
        propName: 'last_name',
        comparator: compEquals,
        refValue: 'Some last name',
    },
];


const filterByRule = (rules, arr) => arr
    .filter((arrItem) => rules.reduce((acc, curr) => acc && curr.comparator(arrItem[propName], curr.refValue), true));

filterByRule(filterRules, someArray);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 KobraKalle