0

假设我的购物车中有 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);

这也最终将数组更改为对象

4

1 回答 1

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']);

于 2015-10-15T12:24:14.860 回答