Object key name with a variable [duplicate] - javascript

This question already has answers here:
Is it possible to add dynamically named properties to JavaScript object?
(20 answers)
Closed 6 years ago.
I am trying to make an object which has one key name taken from a variable.
var key="KeyName"
var obj={
key:"value"
}
When I want to access the "KeyName" key,I can't because I have just created a key with the name "key" not "KeyName".
I found a soution here :
JavaScript set object key by variable
var key="KeyName"
var obj={
[key]:"value"
}
But it doesn't work.
What to do?

You can do it like this: first initialize the object and use brackets [] to set key value.
var obj = {};
var key = "KeyName";
obj[key] = "value";

Related

JS set var value based on key/value pair where key is equal to the var name [duplicate]

This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
Closed 5 years ago.
I am trying to set a variable from a key value pair where the key is the name of the variable.
Let's say I have :
var someValue = 10;
function setValue(key, value) {
key = value; //key is the actual name of the var i.e. "sameValue"
}
So the idea is to change the value of "someValue". Is this even possible in JavaScript?
The better approach is to make use of a JSON object. To achieve that , you can always have a key:value JSON object where the key corresponds to your variable. See the example below. The variable someValue is actually a key of the JSON object obj then you can easily reference that to simulate as if you are calling a dynamic variable and changing its value.
var obj = {
someValue : 10
};
function setValue(key, value) {
obj[key] = value; //key is the actual name of the var i.e. "sameValue"
}
setValue('someValue',30);
console.log(obj['someValue']);

Is it possible to create dynamic keys in a object in javascript? [duplicate]

This question already has answers here:
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
Closed 5 years ago.
for example I ask the user to enter an input and he enters "key" and I save the value on a variable called INPUT.
Then I want to create an object with this like this.
var obj = {INPUT: "some other input"}; where INPUT = key
I know I can add more values but I need to know the key ahead of time in order to add it. Can I add a new key without knowing what it is?
Yes, with computed property names. Assuming INPUT is an actual variable, simply wrap it in [].
var obj = {[INPUT]: "some other input"};
The long version of doing this would be to use bracket notation when adding your keys.
var obj = {};
obj[INPUT] = "some other input";

jQuery use variable as array key [duplicate]

This question already has answers here:
Variable as the property name in a JavaScript object literal? [duplicate]
(3 answers)
Closed 8 years ago.
var diena = "example"
array.push({diena: sub_array});
I want to array key use variable and have key "example" not "diena". How I can do this ?
You'll need to construct the object beforehand.
var diena = "example";
var obj = {};
obj[diena] = sub_array;
array.push(obj);

get value with string key in javascript [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 9 years ago.
I can't figure out how to get an object property using a string representation of that property's name in javascript. For example, in the following script:
consts = {'key' : 'value'}
var stringKey = 'key';
alert(consts.???);
How would I use stringKey to get the value value to show in the alert?
Use the square bracket notation []
var something = consts[stringKey];
Javascript objects are like simple HashMaps:
var consts = {};
consts['key'] = "value";
if('key' in consts) { // true
alert(consts['key']); // >> value
}
See: How is a JavaScript hash map implemented?

How to use the value of a variable in creating an object in JavaScript [duplicate]

This question already has answers here:
How to create an object property from a variable value in JavaScript? [duplicate]
(9 answers)
Closed 9 years ago.
I have an object:
var obj = {fields : []};
And a variable:
var x = "property_name";
Now how do I use the value in the variable x as the property name for the object that I'm going to push inside the array?
I tried something like the one below, but it takes x literally and uses it as the property name. What I want to use is the value stored in the variable x. Is that possible?
obj.fields.push({x : 'im a value'});
You cannot use the object literal syntax for this purpose. However, you can create a new object and then use the [] syntax - remember, obj.xyz is equivalent to obj['xyz'] - but as you can see with the quotes in the latter, you can use an expression there - such as a variable:
var x = "property_name";
var obj = {};
obj[x] = 'value';
obj.fields.push(obj);

Categories

Resources