I want to do something along these lines:
var firstName = "alan";
var secondName = "antonella";
var statusOfPeople = {
firstName: "excellent",
secondName: "superb"
};
console.log(JSON.stringify(statusOfPeople));
And get this output:
{ "alan": "excellent", "antonella": "superb" }
Is this possible? Without doing this?
var statusOfPeople = {};
statusOfPeople[firstName] = "excellent";
statusOfPeople[secondName] = "superb";
No, not directly in the object literal declaration.
It would be impossible to tell if you intended the variable value, or the identifier itself as the property name.
You could create a helper function that takes arrays of key/value pairs if you wanted:
And use it like:
You could even reduce the syntax a bit if you did it like this:
And used it like this: