need help in javascript understanding a line [duplicate] - javascript

This question already has answers here:
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 8 years ago.
var stooge = {
"first-name": "Jerome",
"last-name": "Howard"
};
var properties = [
'first-name',
'middle-name',
'last-name',
'profession'];
for (i = 0; i < properties.length; i++) {
console.log(properties[i] + ': ' + stooge[properties[i]]);
}
I don't understand stooge[properties[i]]. Why are we using bracket before properties?
Can someone explain when to use brackets?

I dont understand stooge[properties[i]].
It means more or less literally what it says.
If i is 0, then properties[i] is properties[0], which is set to 'first-name'.
Therefore, stooge[properties[i]] is stooge[properties[0]] is stooge['first-name'] is "Jerome".
EDIT
As someone pointed out, you cannot use dot-notation here. The name of the property is first-name. If you typed stooge.first-name, the parser would interpret that as stooge.first - name. undefined minus undefined is... NaN!

This is just a way to access properties dynamically from an object. In this cause since there is an array of strings, it lets you get the value from the object. Since you can't do something like obj.'some-string'
This might help JavaScript property access: dot notation vs. brackets?

Object properties can be accessed with a .. or with [].
But dot notation won't work here.
because if you switch stooge[properties[i]] to stooge.properties[i] it would return undefined, because the stooge object doesn't have a member named properties.

Related

Returning Value of property in Javascript [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 3 months ago.
Below is a simple Object I created in Javascript.
var obb = {name : "Charlie", Age : 28 , Location : "London" , Job : "Detective"} ;
var x = "name";
console.log(obb.name);
console.log(obb.x) ; //Dot notation :- returns value undefined
console.log(obb[x]); // Square bracket :- returns correct answer
I know that there are two methods to fetch values of objects i.e dot notation and square bracket.Now if I am storing value of property in a variable & using dot notation with the variable to fetch value , why is it not working ?
In JavaScript, the object attributes can be accessed using either the dot notation or the bracket notation. Dot notation is frequently used because it is simpler to read and understand. What is the significance of bracket notation and why should we use it? The square bracket syntax [] turns the expression within to a string, allowing us to access object properties via variables.

Concat string in $scope angularjs [duplicate]

This question already has answers here:
Variable as the property name in a JavaScript object literal? [duplicate]
(3 answers)
Closed 6 years ago.
I want to pass string name through argument in function and concat in $scope, this is possible?
function test(txt){
$scope.person. + txt; // I want to do $scope.person.status; for example
}
Thanks.
Yes, just do:
function test(txt){
$scope.person[txt];
}
Check Property accessors.
As any object you can access their properties using Dot notation or Bracket notation. When you don't know before hand what property you would be accessing you would use Bracket notation ideally.
function test(txt){
$scope.person[txt] = ''; // We need to assign something to it if not it wont work;
}
Demo example : https://jsbin.com/gihizo/3/edit?html,js,console,output

Why I can't access JSON dictionary element using dot notation? [duplicate]

This question already has answers here:
Object property name as number
(6 answers)
Closed 6 years ago.
I have a following JSON representation:
var collectionCopy = JSON.parse(JSON.stringify(
{
1 : {
2: "2"
}
}
));
Why cant I access key "2" using dot notation (i.e. collectionCopy.1.2) ?
You can use the dot notation for accessing an object's properties only on a valid identifiers in the language.
And since numbers (or anything that starts with a number) are not a valid identifiers you can access it (as a property of an object) only with the bracket notation.
This is because the keys are strings not actual numbers:
to access it use:
collectionCopy[1][2]
or
collectionCopy['1']['2']
Relevant docs on accessing properties

how to use . point into javascript variable name [duplicate]

This question already has answers here:
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 7 years ago.
I have and API that return an Array of object like the following:
{
"form_submit.form_submit_id": 7987,
"form_submit.exported": false,
"form_submit.updatedAt": "2016-01-18T16:13:16.813Z",
"form_submit.user.user_id": 14,
"form_submit.user.name": "Hugo Bismarck",
},
If I use the . on my javascript for example to get the first element
var test = array[0].form_submit.form_submit_id;
get the following error
array[0].form_submit.form_submit_id is undefined
the problem is that the name of the attributes have points, so how can acces to this attributes?
You can use in this case the bracket notation:
property_name is a string. The string does not have to be a valid identifier; it can have any value, e.g. "1foo", "!bar!", or even " " (a space).
var test = array[0]['form_submit.form_submit_id'];

what is a difference myObj.a=b vs myObj[a]=b [duplicate]

This question already has answers here:
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 8 years ago.
I was reading here on stack overflow that these are not equal. So what is the difference.
What happened is that in 2nd case value was assigned as the property of myObj. So if b='abc';
then myObj.abc was now available.
I had always thought same thing but [] version was used when name were weird ones.
Dot notation takes an identifier that is the property name. The square bracket notation accepts a string representation of the property name.
Given var a = "a"; then myObj.a = b and myObj[a] = b and myObj["a"] = b are equivalent.
The difference between myObj.a=b and myObj[a]=b is that in the first case you are accessing an attribute called a in the object. In the second you are accessing an attribute whose name is in a variable called a.
On the other hand, myObj.a=b and myObj["a"]=b would be equivalent.
a lot, results would depends on a var value. but ["a"] would be the same as .a

Categories

Resources