I have one object var tree={} with attribute tree.leaves.leaf={}.
When I perform tree.hasOwnProperty("leaves.leaf") its giving false .
Can I use dot function inside hasOwnProperty() function? How to do it?
If you want to create a property with key leaves.leaf, then you need to use bracket notation
tree["leaves.leaf"]={}
now tree.hasOwnProperty("leaves.leaf") will give true.
You have to use something like below
var tree = {}
tree["leaves"]={}
tree["leaves"]['leaf'] = {}
tree.leaves.hasOwnProperty("leaf")
Related
I know we can access data using dot notation and bracket notation but here if I access faker data using bracket notation I can't access that data. Is there any way to access data using bracket notation?
var faker = require("faker");
console.log(faker.name.findName()); // Output: any name.
console.log(faker["name.findName"]()); // Output: TypeError: faker.name.findName is not a function
The problem is that
faker["name.findName"]
would try to access a property called "name.findName" on the faker-object.
In order to access findName using bracket-notation, you need to change it to:
console.log(faker["name"]["findName"]());
You shouldn't use bracket notation to return functions.
console.log(faker["name"].findName())
or if you really just want to use brackets, try
let _findName = faker["name"]["findName"];
console.log(_findName());
I have an object like this:
let object = {
seniority: 'director_level',
'address.long_form': 'Phoenix, AZ, United States'
}
when I go to print address.long_form like this I get an error.
console.log(object.address.long_form)
How do get the value for 'address.long_form'?
Since address.long_form is in string, you cannot use dot notation instead use this
console.log(object['address.long_form'])
It will work like this: object["address.long_form"], but just don't use dots in object indexes.
You can still extract it but you can't use the literal notation, you need to use the [] notation in order to get past the ambiguity.
i.e.
console.log(object['address.long_form'])
Of course better would to just avoid keys in that form, but depends on your data.
I am passing an object to a function. Simplified example below:
countyInfo(sales.NY2008);
The object is:
{countyName:"Albany",percentage:0.864789889,percentageComparison:40.18903649,sales:1222},
{countyName:"Allegany",percentage:0.789529462,percentageComparison:27.98874729,sales:149},
{countyName:"Broome",percentage:1.009475302,percentageComparison:63.64364553,sales:880},
{countyName:"Cattaraugus",percentage:0.874885092,percentageComparison:41.82554597,sales:276},
{countyName:"Cayuga",percentage:0.801267677,percentageComparison:29.89160156,sales:268},
{countyName:"Chautauqua",percentage:0.830185925,percentageComparison:34.5794701,sales:455},
{countyName:"Chemung",percentage:0.744919757,percentageComparison:20.75717391,sales:272},
{countyName:"Chenango",percentage:1.191003494,percentageComparison:93.07074993,sales:242},
{countyName:"Clinton",percentage:0.767315265,percentageComparison:24.38765663,sales:265},
{countyName:"Columbia",percentage:0.83461736,percentageComparison:35.29783949,sales:260},
{countyName:"Cortland",percentage:1.144086442,percentageComparison:85.46513794,sales:234},
It works beautifully.
Now I would like to compose this parameter from variables.
var getLocation = "NY";
var getYear = "2008";
var getParameter= getLocation + getYear;
countyInfo(sales.getParameter)
It doesn't work as I'm passing a string. But how can this be done?
Just change your code to use bracket notation to access object property instead of dot notation
var getLocation = "NY";
var getYear = "2008";
var getParameter= getLocation + getYear;
countyInfo(sales[getParameter]);
Dot notation:
Property identifies can only be alphanumeric (and _ and $)
Propertyidentifiers cannot start with a number.
Property identifiers cannot contain variables.
OK — obj.prop_1, obj.prop$
Not OK — obj.1prop, obj.prop name
Bracket notation:
Property identifiers have to be a String or a variable that
references a String.
It is okay to use variables, spaces, and Strings
that start with numbers
OK — obj["1prop"], obj["prop name"]
For Detailed explanation refer this --
https://codeburst.io/javascript-quickie-dot-notation-vs-bracket-notation-333641c0f781
Since sales seems to be an object, you can use index notation to access a member.
That is, sales.NY2008 is equivalent to sales["NY2008"].
So, simply do
countyInfo(sales[getLocation + getYear]);
function me(a, b) {
function he(c, d) {
if(!a[c]) {
}
}
}
Please someone explain 'if(!a[c])' for me.
why this square bracket is used here in [c] though it is a parameter. it is not an array obviously.
what does if(!a[c]) make sense? how two parameters are used like this?
There is nothing special about that code.
It is saying, in English, If the property c of a is falsey, then the condition is true.
In JavaScript, bracket notation can be used to access properties of an object or members of an array.
For example, someArray[5] will access the 6th member of the array, while someObject['someProp'] will access someProp of the object someObject.
The argument a is likely an object. The syntax:
if (!a[c])
Checks to see if the property in the variable c on the a object does not have a truthy value. This could be if it was null, false, undefined or any other falsey value.
The bracket notation can be used with property names. So, if you have an object like this:
var x = { name: "John"};
Then, you can access that property like in any of these ways:
x.name
x["name"];
// Or, if the property name is in a variable
var prop = "name";
x[prop]
Your example is using a version of the last of the above three options when the property name is in another Javascript variable.
In javascript, properties can be accessed dynamically using the square-bracket syntax.
Consider the following:
var person = {name:'Sarah'};
console.log(person.name); // logs 'Sarah';
Sometimes, you might want to dynamically access properties of an object, using a variable that holds the name of the property you want to access. The above example could also be written like this:
var person = {name:'Sarah'};
var prop = 'name';
console.log(person[prop]); // logs 'Sarah';
In json can we get attribute value by passing variably value . Means
It works for me when "name" attribute exists in my "returnData" json object
// It works
var getColValue= returnedData[0].name
but it give undefined error
// It Not works
var refVar ="name";
var getColValue= returnedData[0].refVar;
var getColValue= returnedData[refVar];
should work. Please give it a try.
Use square bracket notation:
returnedData[refVar];
In other words, these two are basically equivalent:
returnedData["name"] === returnedData.name
Note that, using square-bracket notation allows you to set/get property names that wouldn't be valid with the dot notation. Eg, returnedData.some-prop is not a valid Javascript object, but returnedData["some-prop"] is.