javascript - Could someone help me avoid using eval()? -


i have code:

  var monday = scheduledb({       "monday": true   }).count();   var tuesday = scheduledb({       "tuesday": true   }).count();   var wednesday = scheduledb({       "wednesday": true   }).count();   var thursday = scheduledb({       "thursday": true   }).count();   var friday = scheduledb({       "friday": true   }).count();   $$(".day-selector").each(function (index) {       $$(this).text(eval($$(this).attr("id")) + " classes");   }); 

what i'm trying use value id attribute of element , database lookup it. .attr() returns string, can't variable value from. how can without using eval()?

use better model not need use eval

var days = {  monday : scheduledb({"monday": true}).count(),  tuesday : scheduledb({"tuesday": true}).count(),   ... }; 

than bracket notation:

$$( ).text(days[$$( ).attr("id")]) 

or forgo lookup

var obj = {}; obj[$$( ).attr("id").tolowercase()] = true; $$( ).text( scheduledb(obj).count() ) 

Comments