Set Object Key by variable in JavaScript
The problem
I would like to store the key I want to use in a variable then creating my objects like:
var key = "keyOne";
var obj = {key: someValue};
But, when I try to examine the value and I found the key is "key" instead of the value of the variable key. Is there any way to set the value of the key from a variable?
Solution
You can use []
to set it.
var key = "keyOne";
var obj = {};
obj[key] = someValue;
References & Resources
- N/A
Latest Post
- Dependency injection
- Directives and Pipes
- Data binding
- HTTP Get vs. Post
- Node.js is everywhere
- MongoDB root user
- Combine JavaScript and CSS
- Inline Small JavaScript and CSS
- Minify JavaScript and CSS
- Defer Parsing of JavaScript
- Prefer Async Script Loading
- Components, Bootstrap and DOM
- What is HEAD in git?
- Show the changes in Git.
- What is AngularJS 2?
- Confidence Interval for a Population Mean
- Accuracy vs. Precision
- Sampling Distribution
- Working with the Normal Distribution
- Standardized score - Z score
- Percentile
- Evaluating the Normal Distribution
- What is Nodejs? Advantages and disadvantage?
- How do I debug Nodejs applications?
- Sync directory search using fs.readdirSync