jQuery use variable as array key [duplicate] - javascript

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);

Related

How to replace .Id with a variable string value [duplicate]

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

JS read object from a string [duplicate]

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);

Accessing value of empty key in javascript object [duplicate]

This question already has answers here:
Should I use an empty property key?
(5 answers)
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 4 years ago.
Javascript objects can accept an empty key with some value but how do we access the value of that empty key ?
var obj = {
"": "Name"
}
Try the following:
var obj = {"":"abc"};
console.log(obj[""]);

Object key name with a variable [duplicate]

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";

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?

Categories

Resources