This question already has an answer here:
var geartable = {
shittyboots: {
name: "Shitty Boots",
cost: "500"
},
shittyarmor: {
name: "Shitty Armor",
cost: "1000"
},
shittyhelmet: {
name: "Shitty Helmet",
cost: "750"
}
};
var shops = {
positions: [
[2, 3],
[0, 1],
[2, 4]
],
inventory: [
[geartable.shittyboots.name, geartable.shittyboots.cost, "Available"],
[geartable.shittyarmor.name, geartable.shittyarmor.cost, "Available"],
[geartable.shittyhelmet.name, geartable.shittyhelmet.cost, "Available"]
]
};
function pickRandomItem(gearlist) {
var result;
var count = 0;
for (var item in gearlist) {
if (Math.random() < 1 / ++count) {
result = item;
}
}
console.log(geartable.result.cost);
return result;
}
Hi there. So, my problem, put simply, is that I'm trying to access an index/a property of a parent object property, but when I run the random selector function (pickRandomItem) on geartable and try to access a property of the result, it tells me that geartable.result.cost is undefined. I assume this is because, for some god forsaken reason, JavaScript is trying to find a property 'result' inside of geartable instead of looking for a property with the value of result inside of geartable.
Is there any way around this? I'm at the end of my rope and I can't imagine there is, due to the fact that object nesting is already pretty shifty as-is. I've tried this with arrays in the place of nested objects, but geartable.result[0]... etc still returns undefined.
This is the error in the JavaScript console, if you're curious:
pickRandomItem(geartable);
TypeError: geartable.result is undefined; can't access its "cost" property[Learn More]
Aucun commentaire:
Enregistrer un commentaire