Why is the 'prop' parameter not working?

var data = {

'Queen' : {

    'Album' : 'A night the Opera',

    'Songs' : ['Bohemian Rhapsody','We are the Champions','Dont Stop Me Now']

},

'The Weeknd' : {

    'Album' : 'After Hours',

    'Songs' : ['Blinding Lights','After Hours','Heartless']

}

};

function updateData(name,prop,value) {

if(name === 'Queen') {

    console.log(data['Queen']);

} else if (name === 'The Weeknd') {

    console.log(data['The Weeknd']);

} else {

    data[name] = {prop : value};

    console.log(data);

}

};

updateData(‘Daya’ , ‘Songs’ , ‘Insomnia’);

do you mean this?
you can’t use a variable for a property name inside an object literal
you would need to write obj[prop] = value

1 Like

Im sorry but I still dont understand.
I’ll reframe the question for you.
How do I add an object to an object to an object?

var data = {

'Queen' : {

    'Album' : 'A night the Opera',

    'Songs' : ['Bohemian Rhapsody','We are the Champions','Dont Stop Me Now']

},

'The Weeknd' : {

    'Album' : 'After Hours',

    'Songs' : ['Blinding Lights','After Hours','Heartless']

}

};

function updateData(name,prop,value) {

if(name === 'Queen') {

    console.log(data['Queen']);

} else if (name === 'The Weeknd') {

    console.log(data['The Weeknd']);

} else {

    data[name] =;

    console.log(data);

}

};

As you can see ‘data’ is an object. Lets call that main object
‘Queen’ and ‘The Weeknd’ objects inside ‘data’. Lets call these sub objects.
‘Songs’ ,and ‘Album’ are mini objects.

My question is:
How do I add a new sub object with a mini object with a value in ‘data’

Do you mean that data[name] is also an object?

function updateData(name,prop,value) {
if(Object.keys(data).includes(name)){
return “Artist already exist”
}
/// do this
data[name] = { [prop] : value };

};

1 Like

Can you please explain to me why should a bracket be used when its clearly a variable?
Then shoudnt ‘value’ also have brackets around it?
Thanks for the help though!!

the same reason why we used " data[name] " … when accessing key/property of an object

1 Like