How can I add a new property for each element without overwriting the last property? H2 backgroundColor is overwriting H2 color in this example:
var cssObject = {}
function updateAllCSS(element, property, value) {
cssObject[element] = [property]
cssObject[element][property] = value
}
updateAllCSS('h2', 'color', '#000')
updateAllCSS('h2', 'backgroundColor', '#FFF')
console.log(cssObject.h2.color)
Any help would be amazing :)
cssObject[element] = [property]assigns an array tocssObject[element]with one element.cssObject[element][property]otoh sets the propertypropertyof that array to a certain value. You end up with an array having one element and a custom property with the same name, i.e. your array looks like{0: 'color', 'color': '#000', length: 1}. The MDN JavaScript Guide provides a good introduction to objects and arrays.