0

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

elvaqueroloconivel1
  • 889
  • 2
  • 8
  • 11

2 Answers2

3

To set the property of the object do this:

collection[id][prop] = value;
J. Orbe
  • 153
  • 1
  • 6
1

The issue is prop: "ABBA" is taking literally prop not the variable prop. Change to:

function updateRecords(id, prop, value) {

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

This also solves another problem that you have, which is you overwrite the whole object, instead of just updating the target property.

MrCode
  • 63,975
  • 10
  • 90
  • 112