When it comes to evaluating a string like employee-count = 3
, my main issue arises when dealing with variables that may not have a standard naming convention. While valid variable names pose no challenge, identifiers such as employee-count
leave me slightly perplexed.
Below is an excerpt of my approach for regular variable names:
function evalWithObject(expression, object) {
_.each(object, (value, key) => {
eval(`var ${key} = value`);
});
return eval(expression.replace('=', '==='));
}
I am exploring solutions where I can define a variable within the current scope using object notation. For instance, while I can easily execute object['employee-count'] = true
, the process becomes trickier in scenarios involving unconventional variable names.
Any suggestions on how to overcome this obstacle?