Is there a way to refer to an object by using the input from the user? Something like this:
var item1 = {
name: "cake",
price: 100
}
var item2 = {
name: "scones",
price: 10
}
function test(){
var userInput = document.querySelector(".codeInput").value;
console.log(userInput.price);
}
document.querySelector(".test").addEventListener("click", test);
If a user enters "item1" then 100 should be logged out. So is there a way to have the string "item1" refer to the object item1?
Thanks for the help.