My inputs are: an array of object names and objects. I want to loop through the array for object name and then output something from corresponding object. My code is as follows:
options=["bold","italic"] ;
var bold ={
action: function(){
alert("<strong>just bold</strong>");
},
b_p: "-40px bold"
};
var italic ={
action: function(){
alert("<em>an italic</em>");
},
b_p: "-20px italic"
};
for(i=0;i<options.length;++i)
{
document.write(options[i].b_p);
}
My desired output: “-40px bold -20px italic” but I get an error saying: “undefined undefined”.
Please help me to find out my mistake/ignorance here.
Thanks.
/******/
Update: the problem is solved below 🙂 Thanks communtiy
Instead of passing the names, pass an object array:
Nothing else would really need to change.