What's the simplest way to call a string method with jquery?
If I understand your question correctly, you want to call a method whose name is stored in a string.
If that's the case, you should use square bracket notation instead of (not in addition to) dot notation, and delimit the literal string with quotes:
$["STRINGVALUE"]();
You can also use the variable you defined initially, without quotes:
$[myFunction]();
Related
When to use and not use single or double quotes within functions?
for example:
function convertToInteger(str) {
return parseInt(str); // why do we not use double-quotes here? is this as simple as we never use quotes around arguments when calling a function?
}
convertToInteger("56");
the variables inside the function are called arguments. They serve to store the item you passed.
As you passed it, it will use whatever value you enter, but if you put a value in quotes, you would be setting a fixed value.
Variable value should be in quotes , variable name should not be in quotes .
convertToInteger("56");
or
var datavalue="56";
convertToInteger(datavalue);
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"]);
Consider below example:
a={
'firstProperty': "first",
'secondProperty':"second"
};
console.log(a[[[["firstProperty"]]]]);
by using multiple bracket notation i am able to access the firstProperty. How bracket notation is accessing this property??
You are using a nested array and by using a non string or symbol as value, the value is converted to string.
console.log([[["firstProperty"]]].toString());
Because what you provide as the key in a property accessor expression is converted to string if it isn't a Symbol or a string. console.log(a[[[["firstProperty"]]]]); uses an array of arrays as the property name in the accessor expression. Since that isn't a Symbol, it's converted to string. When you convert your array to string, you get the string "firstProperty" because that's how Array.prototype.toString works:
console.log(String([[["firstProperty"]]]));
...and "firstProperty" correctly identifies one of the properties in the object, so the property accessor expression gives you the value of that property.
Using an array like that is unnecessary. Just use
console.log(a["firstProperty"]);
or
console.log(a.firstProperty);
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.
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.