设置数组内容属性的方法?

时间:2014-01-01 23:51:39

标签: javascript arrays properties undefined

我有一个库存脚本,我正用它来创建一个阵列中玩家库存的读数。我需要设置.amount属性,但是当我尝试将它们设置为这样时,我会收到一些未定义的错误。我不能使用集合或散列图。谁能告诉我我错过了什么?

我现在只使用NaN来调试代码。

    //This is already defined as an array, just a shortcut.//
    var inv = state.history[0].variables.player.inventory;
    inv[i].amount = 0;

    //Do this for every item in the inventory.//
    for (var i = 0; i < inv.length; i++) {

        //If the inventory item is Apple should resolve to ...inventory.Apple.amount.//
        var q = inv[i].amount;

        //If we find a duplicate in the array, just count and keep going.//
        if (inv[i] == inv[i-1]){
            q = (q + 1);

        //If the item is the last one or unique, print the item on the inventory screen.//
        } else {
            q = (q + 1);
            alert(inv[i] + " " + q.NaN);
            new Wikifier(place, inv[i] + " " + q.NaN + "<br>");
        }
    }

1 个答案:

答案 0 :(得分:0)

你试图在循环中定义它之前得到它,把它放在:

之后
//This is already defined as an array, just a shortcut.//
var inv = state.history[0].variables.player.inventory;

//Do this for every item in the inventory.//
for (var i = 0; i < inv.length; i++) {
    inv[i].amount = 0;

    //If the inventory item is Apple should resolve to ...inventory.Apple.amount.//
    var q = inv[i].amount;

    //If we find a duplicate in the array, just count and keep going.//
    if (inv[i] == inv[i-1]){
        q = (q + 1);

    //If the item is the last one or unique, print the item on the inventory screen.//
    } else {
        q = (q + 1);
        alert(inv[i] + " " + q.NaN);
        new Wikifier(place, inv[i] + " " + q.NaN + "<br>");
    }
}