从对象数组返回值的问题

时间:2018-03-26 01:52:19

标签: javascript sorting

我对js很新(几周)并对我正在开发的增量游戏提出疑问。我的问题与从我拥有的对象创建数组然后获取对象的属性有关,该属性在比较语句中使用并在我的HTML中更新。

我有以下名为UPGRADES的对象:

var UPGRADES = {

newClothes: {
    name: "New Clothes",
    desc: "Give your bums a new look and some more motivation! \n Bum 
    production bonus: 100%",
    moneyCost: 1000,
    scienceCost: 10,
    requiredScience: 10,
    buildingAffected: BUILDINGS.bumBuilding,
    upgVal: 2,
    id: 'newClothes'
},   
//{upgrade 2}
//{upgrade 3 etc.}        
}

对于我的代码的一部分,我需要遍历UPGRADES的每个元素,返回第n个对象WITHIN"升级" (将newClothes作为索引0),然后调用(Nth index.scienceCost)。

到目前为止,我已完成以下工作:

var numBuildings = objectLength(BUILDINGS);
var numUpgrades = objectLength(UPGRADES);

function checkVisiblityOnUpgrades () {

var upgArray = [];
for (var a = 0; a < numUpgrades; a++) {
    upgArray[a] = Object.keys(UPGRADES)[a].toString();      
    console.log(UPGRADES.upgArray[a]);

    if (UPGRADES.upgArray[a].requiredScience <= resources.science) {
        var idString = upgArray[a].id.toString();
        getId(idString.concat("Button")).style.visibility = "visible";
        getId(idString.concat("MoneyCostDisp")).innerHTML = 
       numFormat(upgArray[a].moneyCost);
        getId(idString.concat("ScienceCostDisp")).innerHTML = 
        numFormat(upgArray[a].scienceCost);
        }
    }
}

我得到了这个错误:

Uncaught TypeError: Cannot read property '0' of undefined
at checkVisiblityOnUpgrades (game.js:268)
at update (game.js:290)
268 is console.log(UPGRADES.upgArray[a]);

我想知道如何实际抓住我想要的物体的价值。我在checkVisibilityOnUpgrades()中创建了一个数组,因此我可以使用for循环遍历每个升级。

我的另一个问题是:如果我要存储100多个升级实例,将UPGRADES切换到数组而不是自己的对象会更好吗?这样我就可以更轻松地获取价值。

2 个答案:

答案 0 :(得分:0)

使用Object.entries可以大大简化初始逻辑:

Object.entries(UPGRADES).forEach(({ key, thisUpgradeObject }) => {
  // `key` references the outer property, eg., 'newClothes'
  // `thisUpgradeObject` references the inner object
});

所以

Object.entries(upgArray).forEach(({ key, obj }) => {
  const {
    requiredScience,
    id,
    moneyCost,
    scienceCost,
  } = obj;
  if (requiredScience < resources.science) return;
  const idString = id.toString();
  getId(idString.concat("Button")).style.visibility = "visible";
  getId(idString.concat("MoneyCostDisp")).innerHTML = numFormat(moneyCost);
  getId(idString.concat("ScienceCostDisp")).innerHTML = numFormat(scienceCost);
});

答案 1 :(得分:0)

我在这里看到问题: 您创建了一个名为upgArray的数组,但随后尝试访问未定义的UPGRADES.upgArray。您想要写的内容可能是UPGRADES[upgArray[a]]

function checkVisiblityOnUpgrades () {

var upgArray = Object.keys(UPGRADES);
for (var a = 0; a < numUpgrades; a++) {

    if (UPGRADES[upgArray[a]].requiredScience <= resources.science) {
        var idString = UPGRADES[upgArray[a]].id.toString();
        getId(idString.concat("Button")).style.visibility = "visible";
        getId(idString.concat("MoneyCostDisp")).innerHTML = 
       numFormat(UPGRADES[upgArray[a]].moneyCost);
        getId(idString.concat("ScienceCostDisp")).innerHTML = 
        numFormat(UPGRADES[upgArray[a]].scienceCost);
        }
    }
}