说我的购物车中有3件商品,我想删除3件商品中的2件,在第一件商品被删除后,其他商品将不会被删除。 经过一些测试后,我发现在删除产品之前,数据是一个数组,在删除产品后,数组转换成了一个对象。
例如:
Basket {Klant: Object, Orderlines: Array[0]}
添加一些项目后:Basket {Klant: Object, Orderlines: Array[3]}
是3个对象:
{"Klant":{},"Orderlines":[
{"id":"2793","number":1,"membershiptype":"New","lineid":"3521"},
{"id":"2802","number":1,"membershiptype":"New","lineid":"3522"},
{"id":"2803","number":1,"membershiptype":"New","lineid":"3523"}
]}
删除项目后:Basket {Klant: Object, Orderlines: Object}
并且在对象中仍然是2个对象:
{"Klant":{},"Orderlines":{
"0":{"id":"2793","number":1,"membershiptype":"New","lineid":"3521"}
"2":{"id":"2803","number":1,"membershiptype":"New","lineid":"3523"}
}}
我尝试拼接并删除数组,但结果相同。 我希望结果是:
{"Klant":{},"Orderlines":[
{"id":"2793","number":1,"membershiptype":"New","lineid":"3521"},
{"id":"2803","number":1,"membershiptype":"New","lineid":"3523"}
]}
这是我的代码:
function Basket() {
this.Klant = {};
this.Orderlines = [];
Basketdata = null;
// check if we have storage and can use it.
if (typeof (Storage) !== "undefined") {
Basketdata = sessionStorage.getItem("Basket");
total = sessionStorage.getItem("aantalitems");
$('#aantalitems').html(total);
console.log(Basketdata);
if (Basketdata != null) {
mybasket = JSON.parse(Basketdata);
this.Orderlines = mybasket.Orderlines;
this.Klant = mybasket.Klant;
}
}
if (Basketdata == null) {
Basketdata = {};
}
$('a.addcart').on('click', {this: this}, this.addArticle);
$('a.remove_cart_item').on('click', {this: this}, this.deleteArticle);
$('input.ChangeAmount').on('keyup change', {this: this}, this.changeAmount);
}
Basket.prototype = {
constructor: Basket,
deleteArticle: function (event) {
var knop = $(event.currentTarget);
var mainartikel = $(knop).parents('tr.cart_item');
var id = $(mainartikel).attr('id');
var total = 0;
for (var i in event.data.this.Orderlines) {
var line = event.data.this.Orderlines[i];
if (line.lineid === id) {
console.log(event.data.this.Orderlines[i]);
console.log(i);
event.data.this.Orderlines[i].delete = 1;
} else {
total += line.number;
}
}
sessionStorage.setItem("aantalitems", total);
$(mainartikel).fadeOut(function () {
$(this).remove();
});
sessionStorage.setItem("Basket", JSON.stringify(event.data.this));
event.data.this.sendToServer(event.data.this);
},
sendToServer: function (object) {
$.ajax({
type: "POST",
url: '/shop?addtoorder=1',
data: JSON.stringify(this),
dataType: 'json',
databasket: object
}).done(function (data) {
this.databasket.Orderlines = data.Orderlines;
for (var i in this.databasket.Orderlines) {
if (this.databasket.Orderlines[i].delete == 1) {
//here is the delete
//delete this.databasket.Orderlines[i];
this.databasket.Orderlines.splice(i,1);
}
if (this.databasket.Orderlines[i].update == 1) {
this.databasket.Orderlines[i].update = 0;
location.reload();
}
}
sessionStorage.setItem("Basket", JSON.stringify(this.databasket));
$('#totalprice').html(data.total);
if (this.databasket.Orderlines.length == 0) {
this.Orderlines = [];
$('div.content').html('\
<div style="float: left;"><b><a href="../../../shop"><img src="../../lib/items/Checkout/template/go-home-icon.png" width="32" height="32" alt="go-home-icon" style="vertical-align: middle;"> Back to the shop</a></b></div>\n\
<br><br>No items in basket\n\
');
}
});
}};
我到目前为止所做的尝试:
在deleteArticle下
我尝试过更改:event.data.this.Orderlines[i].delete = 1;
至:delete event.data.this.Orderlines[i]
也最终将数组更改为对象
在sendToServer下 我试过了两个
delete this.databasket.Orderlines[i];
this.databasket.Orderlines.splice(i,1);
也最终将数组更改为对象
答案 0 :(得分:0)
尝试:
delete yourObj['Orderlines']
var obj = {Basket: {Klant: Object, Orderlines: Array[0]}};
obj['Orderlines'] = [
{"id":"2793","number":1,"membershiptype":"New","lineid":"3521"},
{"id":"2802","number":1,"membershiptype":"New","lineid":"3522"},
{"id":"2803","number":1,"membershiptype":"New","lineid":"3523"}
];
document.write('After adding to obj[\'Orderlines\']: ' + obj['Orderlines'] + '<br/>');
delete obj['Orderlines'];
document.write('After deleting from obj[\'Orderlines\']: ' + obj['Orderlines']);