cannot access property - JS Error Shown [duplicate] - javascript

This question already has answers here:
How can I access object properties containing special characters?
(2 answers)
Closed 7 years ago.
My Code to access a property:
var myArray = [{"td_stlmnt":"NN2015227","Heading":"NN2015227[02/12/2015]","td_scripcd":"514286","Order#":"1000000003042299","Trade#":"1124070","Time":"14:40:38","Security":"ASHIMASYN (514286)","Buy":"250","Sell":"0","Market Rate":"12.90","Brokerage":"12.50","Buy Value":"3237.5000","Sell Value":".0000","_":"","Ordr":"ASHIMASYNb","BDate":"20151202","Net Value":""},{"td_stlmnt":"NN2015227","Heading":"NN2015227[02/12/2015]","td_scripcd":"514286","Order#":"1000000003042299","Trade#":"1124072","Time":"14:40:38","Security":"ASHIMASYN (514286)","Buy":"250","Sell":"0","Market Rate":"12.90","Brokerage":"12.50","Buy Value":"3237.5000","Sell Value":".0000","_":"","Ordr":"ASHIMASYNb","BDate":"20151202","Net Value":""}];
alert(myArray[2].Order# );
<html>
<body>
</body>
</html>
Getting an JS Error Uncaught SyntaxError: Unexpected string ->on alert - when I code as .Order# and if I use .Order then value is undefined.
what could be the possible solution ??

You have to use the bracket notation : json[2]['Order#']
You can't use # out of a string
var myArray = [{"td_stlmnt":"NN2015227","Heading":"NN2015227[02/12/2015]","td_scripcd":"514286","Order#":"1000000003042299","Trade#":"1124070","Time":"14:40:38","Security":"ASHIMASYN (514286)","Buy":"250","Sell":"0","Market Rate":"12.90","Brokerage":"12.50","Buy Value":"3237.5000","Sell Value":".0000","_":"","Ordr":"ASHIMASYNb","BDate":"20151202","Net Value":""},{"td_stlmnt":"NN2015227","Heading":"NN2015227[02/12/2015]","td_scripcd":"514286","Order#":"1000000003042299","Trade#":"1124072","Time":"14:40:38","Security":"ASHIMASYN (514286)","Buy":"250","Sell":"0","Market Rate":"12.90","Brokerage":"12.50","Buy Value":"3237.5000","Sell Value":".0000","_":"","Ordr":"ASHIMASYNb","BDate":"20151202","Net Value":""}];
document.write(myArray[1]['Order#']);

Related

Assign dynamic index in array of object in 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.
If I have array of object in such a way.
data[0].name= 'Prashant Shukla';
$scope.getColumn[i].Field = 'name';
Using above I want to assign Prashant in $scope.getColumn[i].value in dynamic way. I have try like this
$scope.getColumn[i].value = data[0].+$scope.getColumn[i].Field;
but this give me Uncaught SyntaxError: Unexpected token +
I can get Prashant Shukla by data[0].name but How can I get exactly same by using $scope.getColumn[i].Field in place of name index ?
how can I resolve this error and assign value dynamically?
thanks ahead.
you can use as like as given below:
data[0].name= 'Prashant Shukla';
$scope.getColumn[i].Field = 'name';
console.log( data[0][ $scope.getColumn[i].Field ] ); // 'Prashant Shukla'
$scope.getColumn[i].value = data[0].+$scope.getColumn[i].Field;
Why you have . after the data[0]?
It should be data[0].name +$scope.getColumn[i].Field;

How to escape # in javascript to retrive object property [duplicate]

This question already has answers here:
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 7 years ago.
i have Javascript object as below.
How can i get value for SERIAL#, like i can retrieve *.INST_ID or *.INSTANCE.
how can i escape # and get the value required.
So far i have tried SERIAL#23% but none helped so far.
var t = {"INST_ID":"1","INSTANCE":"xina","SID":"27","SERIAL#":"48810", "PROGRAM":"Perl#app01"}
console.log(kSess.SERIAL%23); gives syntax error
I am parsing variable "t"
This data is coming from java code, so there is nothing much i can do to change SERIAL# to something else
Any idea?
Try to retrieve the value like this...
t["SERIAL#"]; // will return you the value..
Store it somewhere or play with it as you like. :)

Reading Json slash in Javascript [duplicate]

This question already has answers here:
How can I access object properties containing special characters?
(2 answers)
Closed 7 years ago.
I'm reading a JSON file through javascript. I'm having trouble getting today/_text because of the forward slash. I can get today successfully by doing: {{hours.results[0].today}}. How would I get today/_text? I've tried:
today\/_text
today/\_text
today//_text
today\\/_text
{"offset":0,"results":[{"today/_text":"Today:YES","today/_source":"/hours/1","today":"2,3,4"}]}
hours.results[0]["today/_text"] should do the trick!
hours.results[0] returns an object that has that as a key, making that the easiest way to access the property in question.

Jquery is not accepting hypen in the jsp page? [duplicate]

This question already has answers here:
How do I reference a JavaScript object property with a hyphen in it?
(11 answers)
Closed 8 years ago.
I have a field sent by SERVICE which is having hypen in it. Eg., first-name (As JSON object)
But when I try to get the value of that field through the jsp. I am getting a script error.
Please let me know how to access the hypen also in this?
var nameList = msg.RESPONSE.DATA.NAME-LIST;
The above way when I try to access it is throwing script error
A variable or property name with an hyphen is indeed wrong in javascript (Jquery).
However, you can access the "problematic" property like this :
var nameList = msg.RESPONSE.DATA["NAME-LIST"];
I would recommend to rename the property(ies)
without hyphen if you control the content of this response

read JSON data received from the mysql [duplicate]

This question already has answers here:
How can I access object properties containing special characters?
(2 answers)
Closed 9 years ago.
I want to read data in the javascript which are received from the server in JSON format.
I've been using this a lot but now it seems that I hit the wall with this example:
JSON
{
"results": [
{
"MIN(jtable.HIT_VALUE)": "70.200000",
"AVG(jtable.HIT_VALUE)": "124.4077234969",
"MAX(jtable.HIT_VALUE)": "1854.620000"
}
]
}
JAVASCRIPT
How to read this values?
I have tried this
response.results[i].MIN(jtable.HIT_VALUE)
and I'm getting this error:
TypeError: Object #<Object> has no method 'MIN'
Any ideas?
MIN(jtable.HIT_VALUE) is the key and has to be used as such using the square bracket notation like
response.results[i]['MIN(jtable.HIT_VALUE)']
Use it as string:
response.results[i]['MIN(jtable.HIT_VALUE)']
JavaScript interprets the call response.results[i].MIN(jtable.HIT_VALUE) as an attempt to call a nonexistent function MIN.
Consider using this:
response.results[i]["MIN(jtable.HIT_VALUE)"]

Categories

Resources