I'm wondering why I'm not able to assign a key / value pair to a given object in JavaScript using some forms of syntax versus others.
function updateObjectWithKeyAndValue(object, key, value) {
var newPair = {};
// Why does the syntax below need to be used to create the key / value pair?
newPair[key] = value;
// Why do "newPair.key = value;" or "newPair = { key: value }" not work?
var newObject = Object.assign({}, object, newPair);
return newObject;
}
Thank you for your help!