How to dynamically create JavaScript variable based on string value? [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
How can I dynamically create a variable whose name is based on a given string value? For example:
var name = 'jayesh';
var value = 'some value';
// ... do something
console.log(jayesh); // prints 'some value'

You could use an object like this.
var obj = {};
var name = 'jayesh';
obj[name] = 'some value';
var myvalue = obj.jayesh;
You can create a global variable like this:
var name = 'jayesh';
window[name] = 'some value';
You can also use eval but this can cause security issues so use with caution!
var name = 'jayesh';
var evalString = 'var ' + name + ' = "some value"';
eval(evalString);

Related

Split variable into several to get infos [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
I would like to separate this in Javascript and create variables infos1, infos2 ....
So I need a function to separate all the info.
<option value="[infos1][infos2][infos3][infos4]"></option>
var InfosInitial = $('#insert').find("option:selected").val();
var infos1 = ?;
var infos2 = ?;
Thanks you in advance !
You can use Regex to do this. We will use the String.match() function in this example. It should look something like this:
const InfosInitial = '[infos1][infos2][infos3][infos4]';
const info = InfosInitial.match(/(?<=\[)(.*?)(?=\])/gm);
const infos1 = info[0];
const infos2 = info[1];
const infos3 = info[2];
const infos4 = info[3];
console.log(`${infos1}\n${infos2}\n${infos3}\n${infos4}`);
console.log('As an array:', info);
Also, I am just saying but having everything in 4 different variables is inefficient. So you should use the array instead.
this is really hacky but you can add new variables to the window element like this
window['myVar'] = 'Hello, world!'
now you can call and redefine myVar by simply doing this
console.log(myVar)
// 'Hello, world!'
myVar = 42069
console.log(myVar)
// 42069
knowing this you can create a function where it gets the attribute 'value'
and split it between the ] and [ with String.split() remove the brackets and loop through the remainders
your question little unclear to me but I guess this is what you want.
var InfosInitial = "[infos1][infos2][infos3][infos4]";
Array.from(InfosInitial.matchAll(/\[([^\]]*)\]/g)).forEach(value => {
console.log("all = " + value[0] + ", value = " + value[1]);
})

Add HTML elements before and after text coming from the database using js [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Like what you see in the picture, this problem that I tried to solve and I did not know how to search for it, so I drew it. I would like to put a text in the HTML language before every point and every element brings information from the database I mean by the data point is r[4] + r[5]
const column11 = document.createElement('p');
column11.setAttribute('class', 'card-text');
column11.textContent = 'Other' + r[4] + r[5]; // r[4] and r[5] should be wrapped with <code></code>
// Database array
const r = [,,,,'Text', 'Text']
const column11 = document.createElement('p');
column11.setAttribute('class', 'card-text');
column11.textContent = 'Other ';
const code1 = document.createElement('code');
code1.textContent = r[4];
const code2 = document.createElement('code');
code2.textContent = r[5];
column11.appendChild(code1);
column11.appendChild(code2);
document.body.appendChild(column11);

Changing function name with array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Could someone say how to make an array of function names that will change in while loop?
Example of what I want:
var funcNames = ['function1()', 'function2()', 'function3()','function4()','function5()'];
var i = 0;
while( i < funcNames.length ) {
function funcNames[i] {
alert("hello");
}
i++;
}
In the example, the code doesn't work. It only shows how the code should work.
You can think about create new dynamic function name in javascript? like below:
var funcNames = ['function1()', 'function2()', 'function3()','function4()','function5()'];
var i = 0;
var result = [];
while( i < funcNames.length ) {
var funcName = funcNames[i];
var func = new Function("return function " + funcName + "{ alert('hello')}");
result.push(func);
i++;
}
console.log(result);
var firstFunc = result[0]();
console.log(firstFunc()); // invoke one func here

Receive a String in this format: "1-10" and create an array with the amount of numbers in the range [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
How to Receive a String in this format: "1-10" and create an array with the amount of numbers in the range. Print the array to screen using a for loop.
I.E - "1-5" received so they array will be: {1,2,3,4,5}
create for workflow with vCenter orchestrator.
You can split the string into array and then iterate in a loop to get the iteration.
let str = "1-5";
str = str.split('-');
for(let i = parseInt(str[0]); i<=parseInt(str[1]); i++) {
console.log(i);
}
You may use some cool ES6:
Array.range = function(s){
const [start,end] = s.split("-");
return Array.from({length:start-end}).map((_,i)=>i+ +start);
};
Usable like this:
Array.range("1-10") //[1,2,3...]
var input = "1-10"; //SAMPE INPUT DATA.
var foo = input.split("-"); //PASRING INPUT DATA.
var answer = [];
for(var i = foo[0]; i<= foo[1]; i++){
answer.push(parseInt(i)); //MAKE AN ARRAY.
}
console.log(answer);

How to convert text into variable name to print its value using innerHTML? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have a function that writes a text into html document. This text is equal to the values of two variables, and the result is equal to the name of a third variable. How can I convert this final text into a variable name to make the function print it's value?
<script>
function start()
{
var name1 = "name";
var name2 = 3;
var name3 = "Text that I want to write";
document.getElementById('here').innerHTML = name1 + name2;
}
</script>
<div id="here"></div>
<button onclick="start()">
Start
</button>
UPD: There was a tupo var name1 = "test", it should be var test1 = "name"
Try using an object!
<script>
function start() {
var names = {
name1: "name",
name2: "3",
name3: "Text that I want to write"
}
document.getElementById('here').innerHTML = names[names.name1 + names.name2];
}
</script>
<div id="here"></div>
<button onclick="start()">start</button>
I'm assuming that you meant for name1 to equal "name" instead of "text." Then by putting it together, you will get the third variable which holds the value of text you want to print. In this case, you can use the eval() function of javascript.
Instead of
document.getElementById('here').innerHTML = name1 + name2;
use
document.getElementById('here').innerHTML = eval(name1 + name2);

Categories

Resources