Assigning the value of a javascript variable within function arguments? [duplicate] - javascript

This question already has answers here:
Javascript - String concatenation [duplicate]
(2 answers)
Closed 6 years ago.
I'm sending an XMLHttpRequest.
In the open function I want to set some parameters for the request.
req.open('GET', /some/path?someparam=*somevariable*, true)
I want to get someparam to equal the value of a variable, like var somevariable. In fact the variable name might even have the same name as someparam.
How do I get the variable to resolve in this instance?

String concatenation and encodeURIComponent to make sure you are producing a valid URI:
'/some/path?someparam=' + encodeURIComponent(somevariable)
In an ES6 environment you can use template literals:
`/some/path?someparam=${encodeURIComponent(somevariable)}`

Related

Value of function parameter is not getting [duplicate]

This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 3 years ago.
I'm not getting the actual value of my parameter name in the below function. Object.assign taking the function parameter name as a string literal so the resulted JSON object also named as name
see the below code.
Please see the resulted json object i got.
How to fix this?
wrap it with []
return Object.assign(..., {[name]: JSON.parse(...)})

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 turn string into variable [duplicate]

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.

Double String Variable Name (Javascript) [duplicate]

This question already has answers here:
JavaScript Dynamic Variable Names [duplicate]
(6 answers)
Convert string to variable name in JavaScript
(11 answers)
Closed 7 years ago.
I know there are other questions similar to this, but I feel as if they don't answer my question. I would basically like to make Dynamic Variable Names. Let me give you an example:
//Take the strings "new" and "variable" and make them into a variable.
"new" + "variable" = "Hello World";
//So technically the variable name is "newvariable" and has the value "Hello World"
So basically it takes two strings, and combines them into one variable name. How would I go about doing this?
P.S. This is NOT to combine the values of variables, just the names
Write it into an array or object
var arr = {};
arr['new' + 'variable'] = 'Hello World';
then
alert(arr['newvariable']);
or
alert(arr.newvariable);

Get the name property of a variable [duplicate]

This question already has answers here:
Get variable name. javascript "reflection"
(4 answers)
JavaScript: Get Argument Value and NAME of Passed Variable [duplicate]
(7 answers)
Closed 8 years ago.
I was wondering if it's possible to get the name of a variables in javascript or JQuery.
Suppose that I declare a variable in javascript like:
var customerNr = "456910";
If a function receive a variable, how can i return the name of the variable?
For example:
function getNameOfVariable(someVariable){
//return name of someVariable;
}
If I call this function like this:
getNameOfVariable(customerNr);
The function has to return a string whose value is "customerNr".
How can I do this in jquery or javascript? Some kind of reflection?
That is simply not possible!
The passed parameter doesn't even have to have a name. It could be a return value of a function or a raw literal (like "string" or 3).
No, this is not possible. Only values are transferred for primitive data types, not references, so how would you know what the "name" of the variable is? For example:
var customerNr="456910";
var customerNrAfterProcessing=customerNr;
getNameOfVariable(customerNrAfterProcessing);
Would it return customerNrAfterProcessing or customerNr?
However, you can imitate this behavior with objects.
var variableName = someMethodThatDeterminesVariableNameOrJustAConstantIfYouPrefer();
var myVariables={};
var myVariables[variableName]={};
myVariables[variableName].value="456910";
myVariables[variableName].variableName=variableName;
function getNameOfVariable(someVariable){
return someVariable.variableName;
}
getNameOfVariable(myVariables[variableName]);
Obviously, this amounts to a lot more work than just using the variableName variable directly, but it could be necessary in some more complicated situations.
See working with objects in JS reference

Categories

Resources