This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 1 year ago.
Is possible in javascript define an object field name based on the value of a variable INLINE?
for exemple:
const field = "name";
const js = {field:"rafael"};
console.log(js);
the result of this code will be {field:rafael} but the result I want is {name:rafael}.
I know I can do
const field = "name";
const js = {};
js[field] = "rafael";
but i would like to do inline as I initialize the object. Is that possible?
The es6 version of JavaScript allows you to handle this issue, we can use variables while creating the object to dynamically set the property like so:
const field = "name";
const js = {[field] : "rafael"};
console.log(js);
Setting dynamic property keys - https://www.c-sharpcorner.com/blogs/how-to-set-dynamic-javascript-object-property-keys-with-es6
Related
This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 2 years ago.
Trying to use a variable as property when updating a document via Cloud functions.
Maybe I miss the syntax here or is it simply not possible.
var value = 'myValue'
var name = 'myVariable'
admin.firestore().collection('mycollection').doc('mydocument').update({ name : value});
The document is updated but it shows then name = myValue and would like to see in the document myVariable = myValue.
What would be the syntax to achieve that to be flexible?
Got it.
var value = 'myValue'
var name = 'myVariable'
admin.firestore().collection('mycollection').doc('mydocument').update({ [name] : value})
This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
Closed 6 years ago.
I want to be able to take JSON into my program and then create objects based off of that data. So, I want to create objects where the name of that object is the value stored in a key value pair. For instance, if I had the following JSON (and I know this isn't perfect JSON):
{
"objectName" : "**variableName**",
"someDataName" : "thatData",
"someOtherDataName" : "thisData"
}
Then I want to be able to make an object like this:
function myObject(thatData, thisData) {
this.name = name;
this.thatData = thatData;
}
var **variableName** = new myObject(thatData, thisData);
The key here is that I want to be able to use the value stored in the ObjectName key value pair as the variable name for the object. Is this even possible? I have been looking for how to do this for a while now. I believe that this is different than "Variable" variables in Javascript? because I am trying to use a value in a key value pair to name my objects.
If I understand correctly what you're trying to do here, one approach may be to set properties within a variable:
let myContainer = {};
// ...
myContainer['whatever_variable_name'] = new ...
You can how use myContainer.whatever_variable_name or myContainer['whatever_variable_name'] to access the new object.
To assign properties from JSON objects, see Object.assign.
You can use the property accessor on the window object to set the value for a dynamic key on the global scope.
var window["**variableName**"] = new myObject(thatData, thisData);
This question already has answers here:
Is there a way to access a javascript variable using a string that contains the name of the variable?
(4 answers)
How to use a string as a variable name in Javascript? [duplicate]
(3 answers)
parse a string as an object from data attribute [duplicate]
(1 answer)
Use dynamic variable names in JavaScript
(19 answers)
Closed 9 years ago.
How can I make the code that follows to work?
var x = 'name';
and then, to use the value inside x, as it was a variable, and set it, so that if i want it to be NAME, i'll have the result:
var name = 'NAME';
Is it possible ?
Not directly, no. But, you can assign to window, which assigns it as a globally accessible variable :
var name = 'abc';
window[name] = 'something';
alert(abc);
However, a much better solution is to use your own object to handle this:
var name = 'abc';
var my_object = {};
my_object[name] = 'something';
alert(my_object[name]);
I haven't seen the rest of your code, but a better way might be to use an object.
var data = {foo: "bar"};
var x = "foo";
data[x]; //=> bar
var y = "hello";
data[y] = "panda";
data["hello"]; //=> panda
I think this is a little cleaner and self-contained than the window approach
This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
jquery dynamic id
(4 answers)
Closed 8 years ago.
i need to access
objectName.myID
but the "myID" part is dynamically generated..
how do i do this?
i tried
this['objectName.'+ variable]
i'd hate to use eval...
ps
this happens in a function (local scope) by the way..
You can access Object properties in two ways:
o.propertyname
//or
o.["propertyname"]
When using the bracket notation you have to put the propertyname in quotes or else it will be interpreted as a variable name (which in your case is exactly what you want). So in your case where you have stored the name of the property as a string, the way to go would be:
var variable = "propertyname";
o[variable];
/* /\ variable is replace with it's string representation "propertyname" */
You can even call methods this way:
var o = {};
var functionname = 'toString';
o[functionname]();
You can mix both notations, your example would look like:
var obj = 'objectName';
var prop = 'myID';
this[obj][prop]
// or this is possible too:
this.objectName[prop]
Assuming propertyName is the name of a variable holding the name of the property, for example 'myId', then you can use.
objectName[propertyName]
More details in the MDN : Working with objects
This question already has answers here:
Is it possible to add dynamically named properties to JavaScript object?
(20 answers)
Closed 3 years ago.
Needing to do:
var some = {
`${foo1}_${foo2}`: bar
}
but this gives a syntax error though I must do it somehow. How?
you can suppose object as hashmap and access properties via []
var foo1 = 'a';
var foo2 = 'b';
var some = {};
some[foo1+'_'+foo2] = 'test';
console.log(some.a_b);