This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 1 year ago.
I currently have the following in my Javascript code where Id is a field of an object:
this.recId = event.detail.row.Id;
I want to make the Id part generic something like
String a = 'Id';
this.recId = event.detail.row.a;
How do I achieve this in Javascipt
Use [ ] instead.
const obj = {
id: "myObject"
}
const a = "id"
console.log(obj[a]);
In your example, it would be
this.recId = event.detail.row[a];
You can use
this.recId = event.detail.row[a] to get the desired result
Related
This question already has answers here:
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
Closed 4 years ago.
CODE:
let name = 'bam';
doc.name = req.body[x].value;
PROBLEM:
I want it to return doc.bam value but actually it get doc.name value while I set name='bam'.
For computed properties you should use []
var doc = {};
let name = 'bam';
doc[name] = "abc";
console.log(doc.bam);
This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 5 years ago.
I need to access to object properties by string with another object
In php you can reach that like if array $b = 't';$a->$b; is the same as $a->t
var obj = {
title : 'spider'
}
var hero = {
spider : 'is a hero'
}
console.log( hero.spider );//works fine
console.log( hero.obj.title );//i need the same result, from variable (obj)
how can i do that
console.log(hero[obj.title])
I think this should work.
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);
This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 8 years ago.
I want to pass a variable into a variable
This is what I want
uk.objects.ARG
I have
var d= ARG
how can I get uk.objects.ARG?
I tried
uk.objects.d
, javascript stores it as the string, not a variable
What should I do in order to make it as a variable ?
I think what you are looking for is perhaps:
var d = "ARG";
uk.objects[d];
You can access JavaScript objects' properties also with the bracket notation:
object[property] = value;
The same works for reading. So in your case you could do the following:
var value = uk.objects[d]
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?