Creating a variable for each key value pair in JS

Solution 1:

Using eval would be the simplest way, there is no need to create a ton of global variables. We can use filter, and destructure each object into its properties which happen to match the condition's logic.

let conditions = "id === 001 || name === 'Albert' && age !== 43"

let data = [
    {id:001, name:"Albert", age:63},
    {id:002, name:"Einstein", age:53}
]

let output = data.filter(({id, name, age}) => eval(conditions));

console.log(output);

Another possibility is to change the structure of conditions so it's code instead of strings, removing the dangers of using eval:

let conditions = ({id, name, age}) => {
  return id === 001 || name === 'Albert' && age !== 43;
};

let data = [
    {id:001, name:"Albert", age:63},
    {id:002, name:"Einstein", age:53}
]

let output = data.filter(conditions);

console.log(output);