Accessing value of empty key in javascript object [duplicate] - javascript

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[""]);

Related

how to destruct this object with customer:id [duplicate]

This question already has answers here:
How to destructure object properties with key names that are invalid variable names?
(3 answers)
How to use special characters (like hyphen) in destructuring assignment syntax? [duplicate]
(2 answers)
Closed 7 months ago.
I have an object with this data structure returned by api:
const obj = {customer:id: '123'}
How should I destruct this object? Thanks

JavaScript object like String literal doesn't to set property [duplicate]

This question already has answers here:
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
How to create an object property from a variable value in JavaScript? [duplicate]
(9 answers)
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Closed 2 years ago.
In JavaScript everything is object like String literal, numbers, Object...
And properties can be added to objects dynamically also, but I am adding property to object which refer to string then it seems it is not working
Following is JS code which I am running
var aVar = "some string";
console.log(aVar);
aVar.name = 'raj';
console.log(aVar.name);
Output...
hi
some string
undefined

how to access property with hyphen in name in JS JSON [duplicate]

This question already has answers here:
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 2 years ago.
Im trying to access an element in JSON With Hyphen in the property name
id("main").textContent = jsonData.res.main-result;
but that is causing an error
const json = {
'property-name': 'something'
}
console.log(json['property-name']);
Use jsonData.res['main-result']

Difference in referencing an object using object.key = value and object['key'] = value [duplicate]

This question already has answers here:
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 6 years ago.
What is the difference between referencing an object with object.key = value and object['key'] = value For example if I had an object call var myObj = {};
I believe the quote notation allows for arbitrary keys, (like console.log({' 1a':3}[' 1a']), whereas the dot syntax can only be used with keys that begin with a letter.

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

Categories

Resources