因此,我是JS的新手,并且正在一个项目中,该项目基于跟踪信息接收产品,然后使用UPC代码,我从AJAX调用php获取包内容,然后将这些项目添加到js中的数组中对象格式,当从包装中的某个项目扫描UPC时,应从包装内容中减去一个,我制作的要减去的功能仅对某些项目对象有效,而并非对所有对象都有效,而且我似乎无法弄清楚为什么,对象之间没有区别,我在做什么错
这是我的代码
//define array
`var packageContent = [];
//create item objects
var Item = function(name, upc, weight, qty) {
this.name = name
this.upc = upc
this.weight = weight
this.qty = qty
}
//with php info add items with the tracking to the package content
function addItemToPackage(name, upc, weight, qty) {
for (var i in packageContent) {
if (packageContent[i].upc == upc) {
packageContent[i].qty += qty;
return;
}
}
item = new Item(name, upc, weight, qty);
packageContent.push(item);
}`
//Package Content Lookup
function initialInboundScan(tracking) {
//define tracking var
var tracking = tracking;
//AJAX request to PHP to find items assosiated with tracking #
$.ajax({
url : "./includes/process.php",
method : "post",
data : {inbound_tracking_scan:1,tracking:tracking},
success : function(data)
{
var items = JSON.parse(data);
//Loop the respone call addItemToPackage()
for(var i in items){
addItemToPackage(items[i].product_name, items[i].UPC, items[i].SKU, parseInt(items[i].qty))
}
return packageContent;
}
});
}
// remove item from package
function remove(upc) {
var upc = upc;
for (var i in packageContent) {
if (packageContent[i].upc == upc) {
packageContent[i].qty --;
if (packageContent[i].qty == 0) {
packageContent.splice(i , 1);
}
}
}
}
remove()函数不适用于使用initialInboundScan()生成的项目;只有用addItemToPackage()直接制成的物品,我似乎无法弄清楚为什么remove()函数不会从数量中减去