【问题标题】:Is it possible to find an index of a randomly-chosen JavaScript Object property (if said property is an array/object)? [duplicate]是否可以找到随机选择的 JavaScript 对象属性的索引(如果所述属性是数组/对象)? [复制]
【发布时间】:2018-10-29 00:26:37
【问题描述】:

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;
}

你好。所以,简单地说,我的问题是我正在尝试访问父对象属性的索引/属性,但是当我在 geartable 上运行随机选择器函数 (pickRandomItem) 并尝试访问结果的属性时,它告诉我 geartable.result.cost 是未定义的。我认为这是因为,出于某种被上帝遗忘的原因,JavaScript 试图在 geartable 内查找属性“result”,而不是在 geartable 内查找具有 result 值的属性。

有没有办法解决这个问题?我已经走到了尽头,我无法想象会有,因为对象嵌套已经很灵活了。我已经尝试用数组代替嵌套对象,但 geartable.result[0]... 等仍然返回未定义。

如果您好奇,这是 JavaScript 控制台中的错误:

pickRandomItem(geartable);
TypeError: geartable.result is undefined; can't access its "cost" property[Learn More]

【问题讨论】:

  • 认为我明白了。你想要geartable[result].cost吗?
  • 你是救生员。谢谢,伙计。

标签: javascript object random properties nested


【解决方案1】:

一个问题是:

for (var item in gearlist) {
  // ...
  result = item;
// ...
console.log(geartable.result.cost);

for..in 循环遍历属性名称(不是值),result 永远不会成为geartable 的属性。如果您想在对象上放置 result 属性,则必须显式分配它:

for (var item in gearlist) {
  if (Math.random() < 1 / ++count) {
    gearlist.result = gearlist[item];
  }
}

然后您以后可以访问gearlist.result。或者,如果您不想改变输入对象,只需分配给result 变量,稍后要访问结果,请稍后访问result 变量名称,而不是geartable.result 属性。

另一个问题是if (Math.random() &lt; 1 / ++count) 测试不平衡 - 较晚出现的对象比较早出现的对象更有可能被选中。可以通过从对象值数组中选择一个随机项来产生平衡的随机结果,只需使用一次Math.random()

const values = Object.values(gearlist);
const randomValue = values[Math.floor(Math.random() * values.length)];
console.log(randomValue.cost);
return randomValue;

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) {
  const values = Object.values(gearlist);
  const randomValue = values[Math.floor(Math.random() * values.length)];
  console.log(randomValue.cost);
  return randomValue;
}
pickRandomItem(geartable);

【讨论】:

  • 是的!情况似乎如此。谢啦。我只是贴错了标签。 geartable[result].cost,而不是 geartable.result.cost。为重复的问题道歉,在任何地方都找不到。
猜你喜欢
  • 1970-01-01
  • 2011-02-01
  • 2021-12-03
  • 2011-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-03
  • 2021-02-06
相关资源
最近更新 更多