Why is the property of JSON object not accessible? - javascript

I'm not able to access progress property of data object. I used debugger to stop the application and check if data object is defined. Here's the screenshot of the console and it completely makes no sense.

data is an Array. You want data[0].progress.

Data is an array, as evidenced by the fact that in the console it has brackets.
In order to access the object inside it, you need to use array index notation, like so:
data[0].progress //0

Related

How to access an object within another object with empty braces (no dots inside) with Javascript?

This code
console.log(window.Joomla.editors);
console.log(window.Joomla.editors.instances);
console.log(window.Joomla.editors.instances.jform_core);
shows me this:
I do not understand why window.Joomla.editors.instances.jform_core is undefined. The only difference I can see is that there are three dots in the braces of window.Joomla.editors. And no dots for window.Joomla.editors.instances. How can I access window.Joomla.editors.instances.jform_core?
{...} means that the object has some properties. {} is an empty object. Therefore when you logged it, it had no proerties at all. The properties you see in the unfolded menu is the live view of the object, so the properties were added after you logged and before viewing the logs.
To resolve this you have to access the properties after they were added, maybe window.onload does help.

Calling a JSON Value by giving the key as variable?

I want to call a JSON value by giving the key via a variable.
Given
if (data[airplane].RunwayThreshold !== undefined){}
Can I make RunwayThreshold a variable?
You can chain bracket access:
data[airplane][RunwayThreshold]
although you may want to check for undefined values along the chain.
Because access is left-associative, that becomes:
(data[airplane])[RunwayThreshold]
and allows you to drill down into objects.

Strange behaviour with angularJS $scope.$parent

I'm have having some trouble accessing a value from the parent scope, and what appears to be some strange behaviour also.
If I log $scope.$parent to the console and inspect the DOM object there is a property topicDiscovery.name. However, when I try to log this ($scope.$parent.topicDiscovery.name) it returns undefined.
Also, when I try to log the topicDiscovery object it returns an empty array, even though when its not empty in the DOM for $scope.$parent.
Why is this?
This means $scope.$parent.topicDiscovery is getting changed after console.log. Google chrome doesnot print all json object when you do console.log. It just refer to current Object. Try using JSON.stringify($scope.$parent), Here you will not get this property as stringify converts that JS object to string and there will not be any memory linking between orignal object and string.
Here is best example.
var d={z:{b:{}}};
console.log(d); //When you check here, d.a_1 is available
console.log("d.a_1",d.a); //Here d.a_1 is not available.
//Here I am adding d.a_1 property
for(var i=0;i<10;i++){
d['a_'+i]=i;
}

Javascript object property logs normally but is undefined?

I have a Javascript object that gets initialised in an asynchronous manner, later edited and then stored in an array until it can be saved. The problem is that one of the checks it goes through is _.has(obj, 'Id'). This test always fails. I've used JSON.stringify() to log the object's internals and everything has its value and looks correct. When I run a simple test of this line with hard-coded values in plunker, everything works as expected. I've tried accessing the Id values through obj.Id and obj['Id'] but these both return undefined and I don't know why. Why can stringify read the property but a regular accessor can't? The Id is a value that is not touched between initialisation and saving of the object.

How can i invoke a method of an object to display some properties of that object in a div?

I'm trying to understand how to leverage the power of a method on an object. It's easy enough to render the properties of my object as long as they are key/value pairs. But when the property is a method, I don't understand how to invoke that method.
If I split up my what I am trying to accomplish, it breaks out like this:
function displayPropertiesOfMangoFruit() { } ... displayPropertiesOfMangoFruit()
My function accomplishes printing out the property of my object and inserting into my div.
http://jsfiddle.net/curlybraces/7hHAR/1/
But when I try to print out the property of my object by making that function a method of it, I have no luck.
http://jsfiddle.net/curlybraces/XH3ML/2/
Can someone explain to me how I can execute a method on my object that will print out some simple html that includes the properties of my object?
You just call it:
mangoFruit.showName();

Categories

Resources