ReferenceError: list is not defined Potsman error [duplicate] - javascript

Hi I need to retrieve a JSON key which contains special characters, my JSON example is as follows.
{"profile":"dev","appconf:[classpath:/application.yml]":{"a.b":value,"x.y":value}
Please can someone provide example code to retrieve value of this key.
appconf:[classpath:/application.yml]

I would use bracket notation:
var value = myJsonData["appconf:[classpath:/appliation.ytml]"];

Related

Invalid syntax not marked so (or not)? [duplicate]

I am learning javascript myself. There is a confusion with some javascript,
price = 14;
name = "Mary";
apples:5; //This line executing without error
"orranges":6; //This line getting error
alert(name);
Those both lines can be used into a json object without any error. But when I am using those lines outside of json object, 2nd line ("orranges":6;) is getting error. Why is that ? And why is not giving error for the first line (apples:5;), is there any way that I can use it outside of json object ?
: isn't an operator, it forms part of label syntax.
See MDN
label : statement
labelAny JavaScript identifier that is not a reserved word.
apples is an identifier.
"orranges" is a string literal.
is there any way that I can use it outside of json object ?
You seem to be confusing JSON with object literal syntax.
You can't use a : as the character that separates a property name from a value in an object when you aren't in the process of defining an object.

Angular JS not parsing nested JSON key with the at (#) in the key name

I am hitting an API which retrieves a nested JSON and setting it to my $scope.data variable.
I do an ng-repeat like ng-repeat="event in data".
and try to access a value in the JSON {{event.src.#userID.title}}
There is an error
Lexer Error: Unexpected next character at columns 14-14 [#] in expression [event.src.#userID.title].
When I forcefully remove the # from the JSON returned from the API and access as {{event.src.userID.title}} it works properly.
Please help so that I can access value with the # in the key name.
The API that I hit returns a list [{"":""},{},{},{}]
{"":""} is a nested list
You have to use a different syntax to access an object property whose name isn't a valid variable name:
{{event.src["#userID"].title}}

Accessing response data using Angular.js with numerical named field

My response object has a field called "50", so right now I'm trying to access and save that data doing something like this:
var thing = $scope.data.array[0].50;
However I'm getting an error on the console when I simply reload the page with the function not even running. When I get rid of the 50, everything is fine. There is indeed a field called "50" inside the $scope.data.array[0] and I do not have access to change the response. Is there something wrong with this because the field is called "50" and maybe JS is interrupting that as a number instead??
Also when I changed "50" to something random like "af", then I get no errors on refresh.
this doesn't work
var thing = $scope.data.array[0].50;
this works
var thing = $scope.data.array[0].af;
The following should work if your first element of the array has a property called "50".
var thing = $scope.data.array[0]["50"];
Property accessors provide access to an object's properties by using the dot notation or the bracket notation.
Syntax
object.property
object["property"]
JavaScript objects are also associative arrays (hashes). Using these you can associate a key string with a value string as shown in the example above.
The reason as to why you don't get an error when accessing $scope.data.array[0].af; is because "af" is valid identifier for a property. Dot notation only works with property names that are valid identifiers. An identifier must start with a letter, $, _ or unicode escape sequence.
For all other property names, you must use bracket notation.

Create a method from a string in JS

So I have a string "getNumber":
I would like to use this string as a method for an object: myObj.getNumber()
Is that possible?
Thanks
As simple as:
myObj['getNumber']();
There two ways to access value from js object .. one is dot notation . and other is square bracket notation [], which allows access to properties containing special characters and selection of properties using variables.
var key = 'getNumber';
myObj[key]();
More information on Mozilla, working with object guide.

How to retrieve a JavaScript property that contains a period

I am using the github API to get my gists.
The actual json returned from github contains a property called 'GetGists.js', how do I get the value of that property?
gistObject.files.GetGists.js returns an error
This is my example page, when you click on the link it will log the object in the console.
gistObject.files["GetGists.js"] should be used. All JavaScript properties can also be accessed through square braces.
Another example:
window.location.href === window["location"].href == window["location"]["href"];
gistObject.files['GetGists.js']
here's the solution
gistObject.files['GetGists.js']
instead of using dot notation, use index notation:
gistObject.files["GetGists.js"]

Categories