Eric Ramos Eric Ramos - 1 month ago 11
Javascript Question

Access variable in a declarion of a object

How are yours? well I hope :)

I have this code

// Setup
var collection = {
"5439": {
"album": "ABBA Gold"
}
};


// Only change code below this line
function updateRecords(id, prop, value) {

if(!collection[id].hasOwnProperty(prop) && value !== ""){
collection[id] = {
prop: value
};
}
return collection;
}



// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");


Focus on the function updateRecords.

I want access to the value prop of my function in the declaration of the object. But the value of prop inside the declaration is prop like a string. How can I change it?

This is the collection after execute the code

var collection = {
"5439": {
prop: "ABBA"

}
};


I dont want prop like a string. I want the value of the variable send by arguments to the function

Answer

To set the property of the object do this:

collection[id][prop] = value;