In Java Script is this correct way to store condition in string and pass it inside IF condition block?

  arrays, filter, if-statement, javascript, object

In Java Script is this the correct way to store a condition in a string and pass it inside the IF condition block? I am doing it for the dynamic filter. For example, sometimes, It applies gender or location or at a time both. I want it to dynamic if there are many filters then I have to apply many conditions manually. is there is any solution to make it dynamic

var condition = `element.gender == "Male"`,
            conditon2 = `element.gender == "Male" && element.location == "Mumbai"` ;

        var person = [
            { name: "Rahul", age: 28, gender: "Male", location :"Mumbai" },
            { name: "Dinesh", age: 24, gender: "Male", location :"Delhi" },
            { name: "Sonam", age: 23, gender: "Female", location :"Kolkata" },
            { name: "Jenny", age: 24, gender: "Female", location :"Banglore" },
            { name: "Mitch", age: 27, gender: "Male", location :"Chennai" }
        ];
        var person_filter = person.filter(function (element) {
            if(condition)
            return element;
        });
        console.log(person_filter);

Source: Ask Javascript Questions

LEAVE A COMMENT