I'm getting that error being reported in firebug:
data = data + "&network=" + rowdata.network + "&10xx=" + rowdata.10xx;
can anyone help me how to fix this error.
rowdata.10xx is invalid JS. Dot notation access of properties uses identifiers and identifiers cannot start with a number. Either use a different property name:
rowdata.x10xx
or use square bracket notation, which lets you use strings instead of identifiers:
rowdata['10xx']
Related
so i am trying to parse some json yahoo finance data
i created this just for testing
but the special char ^ cannot be accessed...
how do you access the json node with special characters
i tried the bracket notation but did not work
with the ["^GSPC"] as suggested but get an error
get Unexpected token ^
var market={"^GSPC":{"symbol":"^GSPC","end":1604001600,"start":1603978200,close:[3330.69,3327.8,3308.83]}}
console.log(market.^GSPC)
any suggestions?
since json key is the special character, you should access using bracket notation, rather than dot.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects#Objects_and_properties
var market={"^GSPC":{"symbol":"^GSPC","end":1604001600,"start":1603978200,close:[3330.69,3327.8,3308.83]}}
console.log(market["^GSPC"])
Instead of calling Object property using Object.key, use the square bracket Object[key] as follows.
You can call object property using key in two ways.
Object.key or Object[key].
var market={"^GSPC":{"symbol":"^GSPC","end":1604001600,"start":1603978200,close:[3330.69,3327.8,3308.83]}}
console.log(market["^GSPC"]);
I ran into a problem using the Pipedrive API. I tried to get some data using the below but it returned an error:
$.each(data.data, function(key,value) {
console.log(value.0d1df598a5539ab5b6b410b339dc9218e0acb091);
});
However this works:
$.each(data.data, function(key,value) {
console.log(value.person_name);
});
Why can't I get values of the keys that are complex strings generated by the Pipedrive system?
To retrieve what you require you would need to use bracket notation as the first character of the property identifier is an integer. Try this:
var value = {
'0d1df598a5539ab5b6b410b339dc9218e0acb091': 'foo bar'
}
console.log(value['0d1df598a5539ab5b6b410b339dc9218e0acb091']);
A possible explanation can be summarized in two part
Valid javascript variable(identifier names)
An identifier must start with $, _, or any character in the Unicode categories Uppercase letter (Lu), Lowercase letter, Titlecase letter (Lt), Modifier letter (Lm), Other letter (Lo), or Letter number (Nl).
In your case the identifier name start with an integer(0)
Property accessors
An key of an object in js can be retrieved either by using dot (.) notation or by using Bracket[]` notation
Square brackets notation allows use of characters that cannot be used with dot notation and also to retrieve an identifier which is not valid according to the first point.Beside it also allows to access properties containing special characters.
This is because js interpreter automatically converts the expression within square brackets to a string & retrieves the corresponding value.Actually
js evaluates the first complete expression with square brackets in a statement, runs toString() on it to convert it into a string and then uses that value for the next bracket expression, on down the line till it runs out of bracket expressions.
So dot notation has marginal upper-hand since it wont go throught he above process.
But it cannot be use it with a variable(or number).
It only allow to access explicit key name of a property
Since the identifier in your object identifiers's name starts with an 0 , bracket notation like value['0d1df598a5539ab5b6b410b339dc9218e0acb091'] will give it's value.
This question already has answers here:
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 5 years ago.
What is the real difference in using [] and . for accessing array or object properties? Which one to use?
Also why doesn't . operator allow the index property?
Accessing members with . is called dot notation. Accessing them with [] is called bracket notation.
The dot notation only works with property names which are valid identifier names [spec], so basically any name that would also be a valid variable name (a valid identifier, see also What characters are valid for JavaScript variable names?) and any reserved keyword [spec].
Bracket notation expects an expression which evaluates to a string (or can be coerced to a string), so you can use any character sequence as property name. There are no limits to what a string can contain.
Examples:
obj.foo; // valid
obj.else // valid, reserved keywords are valid identifier names
obj.42 // invalid, identifier names cannot start with numbers
obj.3foo // invalid, ""
obj.foo-bar // invalid, `-` is not allowed in identifier names
obj[42] // valid, 42 will be coerced to "42"
obj["--"] // valid, any character sequence is allowed
obj[bar] // valid, will evaluate the variable `bar` and
// use its value as property name
Use bracket notation:
When the property name is contained in a variable, e.g. obj[foo].
The property name contains characters not permitted in identifiers, e.g. starts with a digit†, or contains a space or dash (-), e.g. obj["my property"].
Use dot notation: In all other situations.
There is a caveat though regarding reserved keywords. While the specification permits to use them as property names and with the dot notation, not all browsers or tools respect this (notably older IE versions). So the best solution in my opinion is to avoid using reserved keywords for property names or use bracket notation if you cannot.
†: That's also the reason why you can only use bracket notation to access array elements. Identifiers cannot start with digits, and hence cannot consist only of digits.
You should use . when you know the name of the property
var object = {};
object.property = 'whatever';
, use [] when the name of the property is contained in a variable
var object = {};
var property = 'another-property';
object[property] = 'whatever';
As #DCoder added certain object properties cannot be accessed without using the [] notation because their names break the syntax. E.g. properties named class, default, or data-prop-value
Also why doesn't . operator allow the index property? I really want
full reason. Thank you.
Well if that was possible, consider:
var a = 0.5;
Did you mean the number 0.5 or access the 5 element of the number?
See:
Number.prototype[5] = 3;
0[5] //3
0.5 // 0.5
If you allowed the syntax 0.5 to be equal to 0[5], then how do you know what you mean?
It is however possible to use numbers directly with object literal:
var a = {
0: 3,
1: 5
};
Both dot operator and index(bracket notation) operator are used to access the property of an Object. Generally accessing with dot operator is quite faster because accessing variables by window is significantly slower though. But in case of special character
in the variables, you cannot use dot operator as it will give error. For such cases we need to use index operator and pass the variable name as a string format means underdouble quote otherwise it will give undefined error.
e.g-
var abc = {
font-size : "12px"
}
Using dot operator - abc.font-size; //it will give error (Incorrect)
Using index operator - abc["font-size"]; //12px (Correct)
I'm parsing a json object that contains an element named data-config.
ex:
var video = data.element.data-config;
Whenever I parse this element I'm getting this error:
ReferenceError: config is not defined
The ReferenceError doesn't mention data-config but simply config.
Any idea why I'm getting this error?
Is this related with the dash (-) character?
Valid Characters
In general JavaScript, variable/function names can't contain -. They can only contain letters, $, and _ (Underscore)
So...
The error is coming because it's parsing:
var video is equal to data.element.data(valid) minus config
Solution
Because variables can't contain dashes, you need to use what I'm going to call String/Bracket Notation
data.element['data-config']
If you need to do more then one, do
data.element['data-config']['child']
I don't recommend using String/Bracket Notation when you don't have to, it's better practice.
You have to use [] notation when object properties contain special characters
var video = data.element['data-config'];
I find myself needing to convert a string var (in JavaScript) to a variable name that is called when getting an element. My spontaneous solution to this was writing:
this.name = name;
[...]
this.context.drawImage(imageRepository.(this.name), this.x, this.y);
This does not work however, returning "Unexpected token (". Any suggestions?
You're looking for a property name, not a variable name. You can use bracketed notation for that:
imageRepository[this.name]
In JavaScript, you can refer to properties in two ways: Using dot notation and a property name literal (obj.foo), or using bracketed notation and a property name string (obj["foo"]). In the latter case, the string can be the result of any expression, including looking up a property on another object (this.name).
You need bracket notation in this case.
imageRepository[this.name]
The bracket notation evaluates the variable and selects the appropriate property.