2012-08-16 17 views
1

我正在写的一类项目的一些代码,和正在运行到一个问题,用下面的代码:Can .pop()可用于JavaScript中的对象数组吗?

var sellEverything = function(gold) 
{ 
    console.log("You decide to buy EVERYTHING."); 

    var holder; 
    while (storeStock.length > 0) 
    { 
     console.log(storeStock); 
     var curItem = storeStock.pop(); 
     console.log(curItem); 
     console.log("You buy a " + curItem.itemName + " for " + curItem.itemPrice + " gold."); 
     gold -= curItem.itemPrice; 
     console.log(gold + " gold remaining"); 
    }; 
}; 

对于角度来看,storeStock是在它6个对象的数组。 curItem变量在未定义的控制台中出现,所以甚至可以从数组中弹出对象,或者是其他问题?

这里是与storeStock中填充数据:提前

"items": 
    [ 
     {"itemName":"Sword", "itemPrice":100}, 
     {"itemName":"Bow", "itemPrice":240}, 
     {"itemName":"Shield", "itemPrice":120}, 
     {"itemName":"Lance", "itemPrice":300}, 
     {"itemName":"Potion", "itemPrice":50}, 
     {"itemName":"Gem of supreme power", "itemPrice":5}, 
     {"itemName":"Better movie script", "itemPrice":139083882} 
    ] 

很抱歉,如果这是一个很明显的问题。

编辑:好吧,我觉得我需要显示的代码全部为清楚起见:

var itemList = 
{ 
"items": 
    [ 
     {"itemName":"Sword", "itemPrice":100}, 
     {"itemName":"Bow", "itemPrice":240}, 
     {"itemName":"Shield", "itemPrice":120}, 
     {"itemName":"Lance", "itemPrice":300}, 
     {"itemName":"Potion", "itemPrice":50}, 
     {"itemName":"Gem of supreme power", "itemPrice":5}, 
     {"itemName":"Better movie script", "itemPrice":139083882} 
    ] 
}; 

//private class ItemStore 
var ItemStore = function() 
{ 
    var storeStock = []; 

    //array in function 
    var setStockList = function(stockArray) 
    { 
     if (stockArray instanceof Array) 
     { 
      var i = stockArray.length; 
      for (i; i > 0; i--) 
      { 
       storeStock.push(stockArray[i]); 
      } 
      //yes, I could have just done the loop forwards, but wanted to do it this way. 
      storeStock.reverse(); 
     }; 
    }; 

    //array out function 
    var getStockList = function() 
    { 
     return (storeStock); 
    }; 

    var sellEverything = function(gold) 
    { 
     console.log("You decide to buy EVERYTHING."); 

     var holder; 
     while (storeStock.length > 0) 
     { 
      console.log(storeStock); 
      var curItem = storeStock.pop(); 
      console.log(curItem); 
      console.log("You buy a " + curItem.itemName + " for " + curItem.itemPrice + " gold."); 
      gold -= curItem.itemPrice; 
      console.log(gold + " gold remaining"); 
     }; 
    }; 

    return{ 
     "setStockList": setStockList, 
     "sellEverything": sellEverything, 
     "getStockList":getStockList 
    }; 
}; 

另外值得注意的是我如何调用setStockList方法:

store.setStockList(itemList.items); 
+1

考虑:http://jsfiddle.net/userdude/sS2fY/ – 2012-08-16 01:38:12

+0

如果'curItem'变量在控制台中显示为未定义,那么您正在循环中。必须有其他一些导致问题的代码。如果'storeStock'不是数组,那么由于调用'pop',你会在控制台中得到一个TypeError。 – 2012-08-16 01:43:05

+0

_“Can.pop()可用于一个对象数组吗?”_ - 可以在任何数组上使用'.pop()'方法(事实上,通过'.call'或''' .apply'),而不管元素的类型如何。还记得一个JS数组可以容纳不同类型的元素,也就是说,一些元素可能是对象,有些可能是数字等。 – nnnnnn 2012-08-16 02:40:15

回答

2
var itemList = { 
    items:[ 
     {itemName: "Sword", itemPrice: 100}, 
     {itemName: "Bow", itemPrice: 240}, 
     {itemName: "Shield", itemPrice: 120}, 
     {itemName: "Lance", itemPrice: 300}, 
     {itemName: "Potion", itemPrice: 50}, 
     {itemName: "Gem of supreme power", itemPrice: 5}, 
     {itemName: "Better movie script", itemPrice: 139083882} 
    ]}; 

//private class ItemStore 
var ItemStore = function() 
{ 
    var storeStock = []; 

    //array in function 
    var setStockList = function(stock) 
    { 
     if (stock instanceof Array) 
     { 
      for (var item in stock) 
      { 
       storeStock.push(stock[item]); 
      } 

      storeStock.reverse(); 
     }; 
    }; 

    //array out function 
    var getStockList = function() 
    { 
     return storeStock; 
    }; 

    var sellEverything = function(gold) 
    { 
     var holder; 

     console.log("You decide to buy EVERYTHING."); 

     while (curItem = storeStock.pop()) 
     { 
      console.log(curItem); 
      console.log(
       "You buy a " + 
       curItem.itemName + 
       " for " + 
       curItem.itemPrice + 
       " gold." 
      ); 

      gold -= curItem.itemPrice; 

      console.log(gold + " gold remaining"); 
     }; 
    }; 

    return { 
     setStockList: setStockList, 
     sellEverything: sellEverything, 
     getStockList: getStockList 
    }; 
}; 


var store = new ItemStore(); 
store.setStockList(itemList.items); 
console.log(store.getStockList()); 
store.sellEverything(100000); 

http://jsfiddle.net/userdude/sS2fY/3/

+0

问题是,为了留在项目的标题中,我不得不保持模型,我把阵列与推动。 – Tarkenfire 2012-08-16 02:08:14

+0

看我的编辑。你是说你必须用'for'循环遍历一个对象列表(而不是'for each'循环)? – 2012-08-16 02:10:15

+0

注意你的对象文字符号是错误的。这些标签在它们周围没有'''',看看编辑。 – 2012-08-16 02:11:15

1

它看起来像storeStock是一个具有items数组的对象。如果是的话,这应该工作:

var sellEverything = function(gold) 
{ 
    console.log("You decide to buy EVERYTHING."); 

    var holder; 
    while (storeStock.items.length > 0) 
    { 
     console.log(storeStock); 
     var curItem = storeStock.items.pop(); 
     console.log(curItem); 
     console.log("You buy a " + curItem.itemName + " for " + curItem.itemPrice + " gold."); 
     gold -= curItem.itemPrice; 
     console.log(gold + " gold remaining"); 
    }; 
}; 
+0

我刚刚编辑我的帖子以显示更多的代码,但足以说我填充了数组只有.items之前的东西,抱歉造成混乱。 – Tarkenfire 2012-08-16 01:55:20

+0

@Tarkenfire - 不用担心。很高兴你找到了解决方案。 – 2012-08-16 03:39:56

0

storeStock是一个对象与storeStock.items是数组你想

相关问题