Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Generate variables from strings using object notation
var array = ["One", "Two", "Three"], local = {};
//for local scope
array.forEach( function(value, index){
local["number" + value] = index + 1;
});
console.log(local.numberOne) //1
console.log(local.numberTwo) //2
console.log(local.numberThree) //3
//for global scope
array.forEach( function(value, index){
window["number" + value] = index + 1;
});
console.log(numberOne) //1
console.log(numberTwo) //2
console.log(numberThree) //3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment