I asked myself a question to optimize my code in JavaScript. I am currently doing something like this :
Data.json :
{
"House" :
{ "bedroom" : "4" }
{ "kitchen" : "1" }
{ "bathroom" : "2" }
}
Choose.js :
var Data = require('./Data.json');
printData = function(id) {
console.log(getData(id));
}
getData = function(id) {
switch (id) {
case "bedroom":
return Data.House.bedroom;
case "kitchen":
return Data.House.kitchen;
case "bathroom":
return Data.House.bathroom;
default:
break;
}
}
And I would like to know if we could optimize this with a special syntax, for example if we simply have :
var Data = require('./Data.json');
printData = function(id) {
console.log(Data.House.{ id });
}
I know this might be a stupid question for you but it would be helpful if you tell me if it's possible or not. I wish I could avoid very long Switch Cases in my project.
Thanks.