我们在操作数组时,加入数组中是以对象的形式存在,例如:
那么我们会涉及到去重复,去掉为0的数组中的对象,js代码如下:
function getItemList(gid, totalMoney, addNum) { var data = { "SI_GID": gid, "TotalMoney": totalMoney, "TotalNum": addNum }; if (addNum > 0) { if (hotelList.length > 0) { var Ishave = false; var flag = 0; for (var i = 0; i < hotelList.length; i++) { if (hotelList[i]["SI_GID"] == gid) { Ishave = true; flag = i; if (i == 0) { flag = -1; } break; } } if (!Ishave) { hotelList.push(data); } if (flag > 0) { hotelList[flag]["TotalMoney"] = totalMoney; hotelList[flag]["TotalNum"] = addNum; } if (flag == -1) { hotelList[0]["TotalMoney"] = totalMoney; hotelList[0]["TotalNum"] = addNum; } } else { hotelList.push(data); } } else { for (var i = 0; i < hotelList.length; i++) { if (hotelList[i]["SI_GID"] == gid) { hotelList.splice(i, 1); } } } console.log(hotelList); }