Skip to content Skip to sidebar Skip to footer

Dynamic Object Properties Javascript

Having issues with this code block: var name = ''; var nutrients = {}; var tds = document.getElementById('data').getElementsByTagName('td'); name = tds[0].innerHTML; nutrients[nam

Solution 1:

When trying to assign

nutrients[name].val = tds[1].innerHTML;

the nutrients object is still empty, and nutrients["Energy"] (or whatever) will be undefined; throwing an exception when beeing assigned a property. Instead, use

nutrients[name] = {
    val: tds[1].innerHTML
};

Post a Comment for "Dynamic Object Properties Javascript"