0

I have a object:

var someObject={};

and i have a function that adds to the object:

function add(id, firstName){
    //stuff that makes sure id and firstName are correct format and stuff and say if firstName is john and id is I23423
    someObject.id=firstName;
}

but it always output as

someObject{
    id="john"
}

but I want the id to be whatever the user inputs the id as, so the output will be

someObject{
    I23423="John"
}

what do I add/change?

J.doe
  • 47
  • 1
  • 6

1 Answers1

0

If I am understanding what you want, you need to use [] on the object almost like it was an array. I honestly don't know the reason why, but this is how you do it.

function(id,name){
  var obj = {};
  //whatever other things you need;
  obj[id] = name;
  return obj;
  }
  
Jhecht
  • 4,407
  • 1
  • 26
  • 44