In JavaScript 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.
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);