Skip to content

Instantly share code, notes, and snippets.

@ajfarkas
Created January 6, 2015 05:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajfarkas/90bd5f9d851e0fa96995 to your computer and use it in GitHub Desktop.
Save ajfarkas/90bd5f9d851e0fa96995 to your computer and use it in GitHub Desktop.
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