This question already has answers here:
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 3 years ago.
I cannot understand the following code, var formData = {}; I guess defines a object "formData", but why to save each element in the formdata["fullName"]? What is this with []? Isn't it used for array? i confused. Could somebody explain this code? Thank you!
function readFormData(){
var formData = {};
formdata["fullName"] = document.getElementById("fullName").value;
formdata["empID"] = document.getElementById("empID").value;
formdata["salary"] = document.getElementById("salary").value;
formdata["city"] = document.getElementById("city").value;
return formData;
}
In javascript, array keys are defined and referenced with square brackets. Object properties can be defined an accessed the same way or with dot notation.
In your case you do have an object, and it's properties can be accessed using bracket notation.
The following two lines are thus equal:
obj["property"] = value;
obj.property = value;
Related
This question already has answers here:
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 4 years ago.
var str="firstname";
var obj={};
obj.str="john";
I want to create property firstname but i want to create it by the variable name like obj.str not like obj.firstname here problem is obj.str create property str not firstname.
i want to create property like this because it will later help me to create property by joining two string .
Try like this with [] notation.
See MDN when to use dot(.) or bracket([]) notation for javascript.
var str = "firstname";
var obj = {};
obj[str] = "john";
console.log(obj);
This question already has answers here:
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 5 years ago.
I have an array of objects and want to add a new object to it in a for loop. They key of the object is dynamic and inside a variable. How can I do this. My array:
var myKey = "someStringThatIsDynamic";
var myArray[i].myKey = "myValue";
Just use bracket notation.
var myKey = "someStringThatIsDynamic";
var myArray[i][myKey] = "myValue";
That allows you to assign the properties dynamically. With the other words, at runtime.
Note : Using square bracket notation allows the use of characters that can't be used with dot notation:
var foo = myForm.key[]; // incorrect syntax
var foo = myForm["key[]"]; // correct syntax
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.
This question already has answers here:
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 7 years ago.
I have found some sourcecode on a javascript project but couldn't understand a part of code that looks like this:
keystate= {};
document.addEventListener("keydown", function(event) {
**keystate[event.keyCode] = true;**
});
document.addEventListener("keyup", function(event) {
delete keystate[event.keyCode]
});
The first problem I do not understand is the brackets that come after declaring of the object and then especially what the "= true" means? And a more ambigous question would be: is this a part of OOP (object oriented programming)?
The square brackets just allows you to access a property by having its name in a string (rather than in an identifier as you would use in dot notation).
The = is an assignment operator.
true is a boolean literal.
These are all equivalent.
foo.bar = "something";
foo["bar"] = "something";
var property = "bar"; foo[property] = "something";
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?