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

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.

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?

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(...)})

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.

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

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)}`

Can I generate new array with another variable in javascript? [duplicate]

This question already has answers here:
What do square brackets around an expression mean, e.g. `var x = a + [b]`?
(7 answers)
Closed 7 years ago.
function construct(head, tail) {
return cat([head], _.toArray(tail));
}
I'd like to know the role of '[' and ']' above.
Is it an operator?
Is it the array initialize literal?
This question is answered here and on the previous QnA(Use of [square brackets] around JavaScript variables).
I felt this weird because I am not used to javascript.
But this is not a matter any more.
I thought wrong and this is explained. Thanks!
It is initializing a 1 element array to pass to cat here. Brackets can be used to access elements on objects and arrays, and to represent an array literal like you have here.

Categories

Resources