I want to create global variables in main.js using data.js. This would allow me to add as many entries in colors, creating variables in main.js without copy-pasting. This doesn't work and I have no idea what to do, I'm pretty sure this is a good example, I might have spelled something wrong but you get the idea.
//data.js
module.exports = {
colors: {
red: ['10','40','25'],
black: ['50','99','1'],
yellow: ['67','87','53']
}
}
//main.js
data = require(./data.js);
colors = Object.keys(data);
for(i = 0; i < colors.length; i ++){
eval(colors[i]) = data.colors[colors[i]];
}
console.log(black);
//wanted output: ['50','99','1']
Putting the eval part all together fixes it.
//main.js
data = require(./data.js);
colors = Object.keys(data);
for(i = 0; i < colors.length; i ++){
eval(colors[i] + '= data.colors[colors[i]]');
}
console.log(black);
//output: ['50','99','1']
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.