Linked Questions
328 questions linked to/from How to use a variable for a key in a JavaScript object literal?
463
votes
2
answers
513k
views
Creating object with dynamic keys [duplicate]
First off, I'm using Cheerio for some DOM access and parsing with Node.js.
Here's the situation:
I have a function that I need to create an object. That object uses variables for both its keys and ...
123
votes
8
answers
62k
views
dynamic keys for object literals in Javascript [duplicate]
Ok so I'm working away on a project in Nodes, and I've come across a small problem with the keys in object literals, I have the following set-up:
var required = {
directories : {
this....
111
votes
4
answers
113k
views
create object using variables for property name [duplicate]
Is it at all possible to use variable names in object literal properties for object creation?
Example
function createJSON (propertyName){
return { propertyName : "Value"};
}
var myObject = ...
69
votes
3
answers
83k
views
Variable as the property name in a JavaScript object literal? [duplicate]
Possible Duplicate:
How do I add a property to a Javascript Object using a variable as the name?
Use variable for property name in JavaScript literal?
Is it possible to add a variable as the ...
28
votes
4
answers
29k
views
javascript object variable key [duplicate]
I am creating a plugin for Jquery and need to have a variable as a key in an object.
$(selector).animate({self.settings.direction: '+='+self.displacement+'px'}, "slow" , function () {});
this part ...
20
votes
1
answer
12k
views
Adding a key from a variable string (es6) when using spread syntax [duplicate]
I would like to know if there is a clean way to set the value of a key from a string variable when using spread syntax in es6?
Something like the following:
let keyVar = 'newKey'
let newObject = {...
21
votes
1
answer
21k
views
Using Enum as Object keys [duplicate]
I have an Enum:
const ingredients = {
BREAD_BOTTOM: 'BreadBottom',
BREAD_TOP: 'BreadTop',
MEAT: 'Meat',
BACON: 'Bacon',
CHEESE: 'Cheese',
SALAD: 'Salad'
};
...
10
votes
7
answers
3k
views
Is it possible to define a dynamically named property using object literal in JavaScript? [duplicate]
Consider the following
var a = {foo: "bar"};
Equivalent to
var a = {};
a.foo = "bar";
Equivalent to
var a = {};
a['foo'] = "bar";
Equivalent to
var a = {}
var b = "foo";
a[b] = "bar";
Is it ...
8
votes
2
answers
8k
views
Javascript variable object keys [duplicate]
I am attempting to add a variable key, with no luck.
Here's what I got so far:
mysql('translations',{
check: 'element_id',
element_id: element_id,
{'lang-'+lang_id}: value
});
The ...
7
votes
2
answers
15k
views
Setting an Array Element as an Object Property [duplicate]
I am trying to set an array element as an object Property
Simplified example:
var array = ['a', 'b', 'c'];
var obj = { array[1]: 'good' }
Above causes an error.
Update: In fact, I am passing the ...
6
votes
1
answer
9k
views
Can I use constant values in key value pair object in javascript [duplicate]
I would like to define a set of constants, which I then use the values of in another javascript object. I have the below example.
This however doesn't seem to work when I am trying to use 0 as a key ...
6
votes
2
answers
6k
views
Create new object with variable key and value on one line [duplicate]
I'm looking for a simple syntax to allow me to create a new object with one property and set the value of that property. Here's an example. I'd love to be able to do something all on one line.
let ...
4
votes
5
answers
705
views
Javascript puts the variable name as it is inside a json and not its value [duplicate]
I observed a very weird phenomena in javascript and I can't figure out why. Consider this example below
state = "West Bengal"
city = "Kolkata"
country = "India"
some_json = {country: {"city": city, "...
1
vote
2
answers
3k
views
Variable as object key does not work in IE [duplicate]
Fiddle
var json = {name: 'chan'};
var variable = 'age';
$.extend(json, {[variable]: 35});
$('#result').html(JSON.stringify(json));
This method works on most popular browsers except IE, I need to ...
3
votes
1
answer
5k
views
Using a dynamic key to setState in React [duplicate]
From an input field I am sending the value as an argument to the function that sets state. I have multiple input fields so would like to use their name (which is equal to their state key) to then use ...