I am trying to push a variable into a property in a nested object, but Javascript keeps giving me this error: TypeError: Cannot read property 'push' of undefined"
However, if I declare a nested variable and use that to push, it works. Why does Javascript have this behavior?
Is there a way to do this without declaring the nested variable and without declaring an empty array?
var collection = {
5439: {
albumTitle: 'ABBA Gold'
}
};
collection[5439]["track"] = [''];
console.log(collection[5439].track);
// nested = collection[5439]["track"];
// nested.push('c') // this works
// console.log(nest)
collection[5439]["tracks"].push('c');