Created
January 6, 2015 05:44
-
-
Save ajfarkas/90bd5f9d851e0fa96995 to your computer and use it in GitHub Desktop.
Generate variables from strings using object notation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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