Here is a snippet of my JavaScript Code that I want to DRY up:
if(agency == 'abcd')map.entities.push(abcd);
if(agency == 'efgh')map.entities.push(efgh);
if(agency == 'xyz')map.entities.push(xyz);
if(agency == 'pqrs') map.entities.push(pqrs);
if(agency == 'values')map.entities.push(values);
Now in future may have more ifs for different keys coming in. JavaScript doesnt offer a build in HashMap which i can use here. And using arrays and id stuff to make it DRY is too tacky.
Is there a more simpler solution to this? May be something like this
if(agency == 'abcd')map.entities.push(stringToVariable('abcd'));
then I can just use a for and iterate through the keys. I am not sure though this possible at all in JavaScript.
There’s a legitimate case for using
evalhere as long as you knowagencyvalues are clean: