我想使用indexOf來確定是否傳遞它的obj已經在數組中。它似乎沒有工作。以下是將項目添加到數組的代碼。Shopping Basket indexOf
的Javascript:
add: function(id, item, description, price, itemObj) {
if(items.indexOf(itemObj) == 1) {
console.log(itemObj);
items.forEach(function(item){
if(item.id === id){
item.itemCount += 1;
};
});
} else {
console.log(itemObj);
items.push(new basket.Item(id, item, description, price));
};
basket.print();
}
更描述性的,它永遠不會運行if語句,將始終採取傳遞一個新的對象數組。
您有調用函數中的項目,並以與調用不一致的方式使用下面的項目。 – rfornal 2014-12-02 18:09:48
很抱歉,你能解釋一下嗎? – hudsond7 2014-12-02 18:10:28
您在函數中使用'item' ** s **的函數(id,'item',description ...進一步,而不是indexOf的項目,而foreach函數中的'item'的forEach。 – rfornal 2014-12-02 18:12:08