JS turn string into variable [duplicate] - javascript

This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
Closed 5 years ago.
I have a json feed that returns an array of google map points. The issue is that it returns everything as a string. I send icon to a google map script. In the example below you will see SQUARE_PIN. This is a variable not a string and it adds the quotes around it preventing it from rendering the variable. Is there a easy way of fixing this.
{
"title":false,
"lat":"44.7930232",
"lng":"-89.7031784",
"icon":{
"path":"SQUARE_PIN",
"fillColor":"#FF0000",
"fillOpacity":1,
"strokeColor":"",
"strokeWeight":0,
"micon":"<\/span>"
}
}

Let's suppose you have your JSON stored in an object called obj. Also, let's suppose that the variable you intend to use is inside another object, called obj2 (could be window if the variable is global). In this case you can do this:
obj.icon.path = obj2[obj.icon.path];
and then use obj.

Related

In JavaScript what is this method called where you might define a variable or property as such variable_name$ref [duplicate]

This question already has answers here:
Rules for unquoted JavaScript Object Literal Keys?
(6 answers)
What characters are valid for JavaScript variable names?
(12 answers)
Closed 1 year ago.
I am seeing it crop up more and more in code I am going through on a new project (can't share due to contractual reasons) where Ill see something like:
{
prop1: value$ref,
$prop2: null
}
I have see ${prop3} before, but never an example without the brackets. Can anyone provide direction as to what the method is, or the operator is or whatever the case?

How to make string to object or array format [duplicate]

This question already has answers here:
Access a nested property with a string [duplicate]
(5 answers)
Get global variable dynamically by name string in JavaScript
(6 answers)
Closed 4 years ago.
I have to create a string in format like below
"currentState[0]['children'][1]"
But I need to execute it later just like below
currentState[0]['children'][1]
I have elements and childrens on currentState. But while looping I have to create a string. But later I need to execute as array.
I have tried almost all array methods. Array.call, bind etc. And string methods as well. Could not get the output
How can I make it
Please be more specific with your question but from my understanding, you can use javascript's eval() function to execute a string as javascript, so when you need to execute it, just run eval("currentState[0]['children'][1]").
The alternative to the eval() would be
function evalFn(obj) {
return Function('"use strict";return (' + obj + ')')();
}
evalFn("currentState[0]['children'][1]")
refer to: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval for a more in-depth explanation.

JS Array called "name" behaves strangely [duplicate]

This question already has answers here:
Is variable called "name" always defined in Javascript?
(2 answers)
Closed 6 years ago.
I played around a bit and created a javascript array containing some strings. When I tried to access the array it behaves quite strangely. The array is created correctly and works as expected if it is called something else.
var name = ["foo", "bar"];
alert(name); // shows "foo,bar"
Why is the array converted into a string, if the variable name is name? According to the standard (which the linked website is based on) it should be a valid variable name: https://mothereff.in/js-variables#name
If you execute javascript in a browser environment, the code is executed in the window context. This context already has some global variables set. One of those is the window.name which is a string. When the variable is set, the browser will automatically cast the new value to string which causes the strange behavior.
So even though name is a valid variable name, it should not be used in the global context if you are executing your javascript in the browser (It should work fine in e.g. node.js).

JAVASCRIPT: method assigned to another method of same object - changing value of first changes second one [duplicate]

This question already has answers here:
javascript pass by reference workaround
(2 answers)
Closed 6 years ago.
So i have an javascript object called gamewhich has two methods:
function game() {
this.TxtRevealed = new Array();
this.TxtRevealedBackup = new Array();
[...]
}
Now, outside an object i assign one two another:
game.TxtRevealedBackup = game.TxtRevealed;
After a while i change game.TxtRevealed(i use slice function to cut some values from it).
And now happens something i do not intend: automatically game.TxtRevealedBackup changes also to new value of game.TxtRevealed.
I'd expect that game.TxtRevealedBackup would be same as game.TxtRevealed was in moment of assigning. It works as if game.TxtRevealedBackup was pointing to value that is represented by game.TxtRevealed continously, not the value it was in moment of assignment.
Why is it happening and how to make it working i'd expect?
Kalreg.
Objects do work by reference. If you want to clone objects see this answer.

Javascript use variable name to thread a function? [duplicate]

This question already has answers here:
Use JavaScript variable as function name?
(5 answers)
Closed 7 years ago.
In GSC, you are able to make a variable become the name of a function that you thread. It looks like this:
variable = "pizza";
[[variable]]();
the engine then reads it like:
pizza();
my question is, is it possible to do that in javascript as easily or do I have to make if/else/switch statements for it?
my question is, is it possible to do that in javascript as easily or
do I have to make if/else/switch statements for it?
If you want to use the safe, fail-proof way, then you can access such variables only in two contexts.
If the variable is in global context, in the case of which, you can do window[variable]();
Else if the variable is a property of an object, in the case of which, you can do obj_name[variable](), basically anything that can be accessed via bracket notation. window is an object too.
Then there's always the dirty way:
You can use highly evil eval like eval(variable + "()") or you can use the Function constructor in the same way. Note however that both the methods can be misused and are highly advised against.

Categories

Resources