JavaScript对象添加键值,相同方法,不同结果

时间:2019-04-08 10:07:14

标签: javascript

感谢阅读我。请原谅我的英语,我的问题也许很愚蠢。 我正在为电子商店设置购物车。我正在尝试从显示的产品中获取数据以创建订单清单。购物车是一个具有3个equals方法的对象,用于添加数量,名称和价格。但是只有一项工作。另外两个覆盖数据。

我要存储的数据是字符串。

var Cart = function () {

    this.cartListQte = {}

    this.products_names = {}

    this.products_prices = {}

}

// class

Cart.prototype = {

    //properties

    'cartListQte': {},

    'products_names': {},

    'products_prices': {},

    //methods

        //setter getter

    setCartListQte: function (cartListQte) {

        this.cartListQte = cartListQte;

    },

    getCartListQte: function () {

        return this.cartListQte;

    },

    setProducts_names: function (products_names) {

        this.products_names = products_names;

    },

    getProducts_names: function () {

        return this.products_names;

    },

    setProducts_prices: function (products_prices) {

        this.products_prices = products_prices;

    },

    getProducts_prices: function () {

        return this.products_prices;

    },

        //"push" new product

    pushed: function (productId,Qty){

        this.cartListQte[parseInt(productId)]= parseInt(Qty);

    },


    pushName: function (productId,nm){

        this.products_names[parseInt(productId)]= nm;

    },


    pushPrice: function (productId,prx){

        this.products_prices[parseInt(productId)]= parseInt(prx);

    }

}

// gestion du panier

function addProductToCart(事件){

//nouveau panier

var cart = new Cart;

    //on récupère le panier déjà enregisté

var curentCart = JSON.parse(localStorage.getItem('panier'));

    //si il existe, on le manipule

if(curentCart != null){

    cart.cartListQte = curentCart.cartListQte;

}

//récupération des nouvelles données à enregistrer

var id = $(this).attr('productid');

var quantity = $("[productIdQte="+id+"]").val();

console.log(quantity);

var name = String($(".name"+id).text());

console.log(name);

var price = $(".price"+id).text();

console.log(parseInt(price));

    //poussées dans le tableau

if(quantity>0){

    /* exemple who don't work too

    var quantity = "1";

    var name = "a";

    var price = "1" ;

    */
    cart.pushed(id, quantity);

    cart.pushName(id, name);

    cart.pushPrice(id, price);

    //écrasement des données dans le local

    localStorage.setItem('panier', JSON.stringify(cart));

    console.log(cart.cartListQte); 

}

我希望输出

cart {

  cartListQte: Object { 5: 4, 6: 3 }

  products_names: Object { 5: "toto", 6: "titi" }

  products_prices: Object { 5: 100, 6: 150 }

}

但实际输出是

cart {

  cartListQte: Object { 5: 4, 6: 3 }

  products_names: Object { 6: "titi" }

  products_prices: Object { 6: 150 }

}

2 个答案:

答案 0 :(得分:0)

问题是您要替换对象中的内容。

setProducts_names: function (products_names) {
    this.products_names = products_names;
    // all names are replaced with one actuall setting
},

因此需要使用三点运算符更新代码,该运算符将复制先前存储的对象内的所有属性

setProducts_names: function (products_names) {
    this.products_names = {...this.products_names,...products_names}; 
    // products_names will be merged
},

无需将此方法添加到每个set...方法中

答案 1 :(得分:0)

There you go, this should do the trick. You forgot to update the 2 other arrays.

if(curentCart != null){

    cart.cartListQte = curentCart.cartListQte;
    cart.products_names = curentCart.products_names;
    cart.products_prices = curentCart.products_prices;

}