简体   繁体   English

JavaScript Object add-key值,Same方法,结果不同

[英]JavaScript Object add-key value, Same method, different result

thanx for read me. thanx给我读。 Excuse my english and my question maybe stupid. 请原谅我的英语,我的问题也许是愚蠢的。 I'm setting up a cart for a e-shop. 我正在为电子商店设置推车。 I'm trying to catch data from products displayed for create a orderlist. 我正在尝试从显示的产品中捕获数据以创建订单列表。 The cart is an object with 3 equals methods for add quantity, name and price. 购物车是一个对象,有3个等于添加数量,名称和价格的方法。 But only one work. 但只有一项工作。 Two others overwrite data. 另外两个人覆盖了数据。

Datas that i want to stock are strings. 我想要存储的数据是字符串。

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 // gestion du panier

function addProductToCart (event) { function addProductToCart(event){

//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); 

}

I expect the output of 我期待输出

cart {

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

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

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

}

but the actual output is 但实际输出是

cart {

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

  products_names: Object { 6: "titi" }

  products_prices: Object { 6: 150 }

}

The problem is that you are replacing content in objects. 问题是您正在替换对象中的内容。

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

so the code needs to be updated with three-dot operator which copies all attributes within the previously stored object 因此需要使用三点运算符更新代码,该运算符复制先前存储的对象中的所有属性

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

This needst o be added to every set... method 这需要添加到每个set...方法

There you go, this should do the trick. 你去,这应该做的伎俩。 You forgot to update the 2 other arrays. 您忘了更新其他2个阵列。

 if(curentCart != null){ cart.cartListQte = curentCart.cartListQte; cart.products_names = curentCart.products_names; cart.products_prices = curentCart.products_prices; } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM