Am stuck trying to add a function to add a new object to a const in javascript
const phonebook = {
"Lily": {
mobile: "+16235554420",
work: "+12445552374",
home: "+19775552223"
},
"Kyle, el hermano de Tim": {
mobile: "+4235555616"
}
};
function addPhoneNumber(name, numberType, number) {
// write your code here
}
// when the function is ready, add the phone number of xx
addPhoneNumber("Stephanie Noland", "mobile", "+4235555212");
console.log(phonebook["Stephanie Noland"].mobile); // "+4235555212"
I am aware that I can initiate a new object via
phonebook[name] = {}
But other than that I have trouble understanding how to add properties on that new object.
Normally a simple:
phonebook[name] = name,
phonebook[numberType] = numberType,
phonebook[number] = number
But that approach would only generate the following which is not what I need:
const phonebook = {
mobile: "+16235554420",
work: "+12445552374",
home: "+19775552223"
As I need those to be under new objects with the name variable.