I'm using Ajax in order to get a list of items of an inventory, I would like to have an object named inventory and then create attributes named as the items of the response, so that means if I insert more items in my database I wouldn't need to hard code the elements of the object in my javascript file. Right now I'm able to create the object, but I can't access de dynamically created attributes and their values. I would like to alter the values with some user Input like pressing a button.
I've tried to use a Dictionary and searched but that didn't seem to work
var inventory = {};
$.ajax({
url:"phpf/getItems.php",
type:"POST",
data:{},
success: function (data) {
var result = JSON.parse(data);
for (var index = 0; index < result.length; index++) {
var str = String(result[index][0]);
inventory[str] = 5;
}
}
});
console.log(inventory["Cookies"]);
console.log(inventory[0]);
I would like to access the information of the object like inventory["something"] but console says it's undefined, and when I try to add the value to another number it says NAN as result