0

JavaScript function below does not use the second parameter to update the object.

var user11553 = {username:"JStudent01"};
var instagram = "JMann22";
var twitter = "JohnM22";


var updateSocialMedia= (obj,str,str2) => {
  obj.str=str2;
};
updateSocialMedia(user11553,'instagramID',instagram);

The output I am getting is { username: 'JStudent01', str: 'JMann22' } which has added a new key value pair and used the str2 argument but not the str agrgument.

Ryan Schaefer
  • 3,047
  • 1
  • 26
  • 46
graveltrunk
  • 75
  • 2
  • 6
  • 1
    Possible duplicate of [Add a property to a JavaScript object using a variable as the name?](https://stackoverflow.com/questions/695050/add-a-property-to-a-javascript-object-using-a-variable-as-the-name) – melpomene Mar 07 '18 at 14:32

2 Answers2

0

You can use

obj[str] = str2

it says, let's find the key : str in my map : obj and change the value to str2

Izio
  • 378
  • 6
  • 15
0

The issue is with the assignment to the object. You are using: obj.str=str2; where str is processed as a property of obj.

Instead, you want to use the property stored in str. Therefore you should use:

obj[str]=str2

Bas van Dijk
  • 9,933
  • 10
  • 55
  • 91