【问题标题】:Adding new object in local storage overwrites the old ones在本地存储中添加新对象会覆盖旧对象
【发布时间】:2016-03-25 14:29:35
【问题描述】:

   
var  product,    
   numberof,   
   quantity,  
   price,  
   localStorageProd;

function storeProdStorage( product , numberof, quantity, price ) {
    var itemsArray = [];
    newItem = {
      product : product ,
      numberof : numberof ,
      quantity : quantity ,
      price : price 
    };
  
    if (localStorage["prodbank"] != null) {
      var localStorageContentObject = JSON.parse(localStorage["prodbank"]);
      itemsArray.push(localStorageContentObject);

      for( var i = 0; i < itemsArray.length ; i++ ) {
        if(itemsArray[i].product == product) {
            itemsArray[i].quantity = quantity; 
            itemsArray[i].price = price;
            localStorage.setItem('prodbank', JSON.stringify(itemsArray[i]));  
        } else {
          itemsArray.push(newItem);
        localStorage.setItem('prodbank', JSON.stringify(itemsArray));  
        }
      }    
            
    } else {
      localStorage.setItem('prodbank', JSON.stringify(newItem));  
    }
}  
  
$(function() {
    $(".quantity-slider").slider({
        range: "min",     
        value: 50,
        min: 50,
        max: 1000,
        step: 50,
        
        slide: function(e,ui) { 
          var quant = $('.quantity').html(commaSeparateNumber((ui.value) *10)); 
          var price = $('.price').html(ui.value);
             if($('.price').html() == "50" ) {
                $('.quantity').html(250);
             }
      
       product = $(".pricing-title").html();
       numberof = $(".pricing-code .form-section h3").contents().get(0).nodeValue;  
       quantity = $(".pricing-code .form-section h3 .quantity").eq(0).html();  
       price = $(".pricing-code .form-section h3 .price").eq(0).html();
          
       storeProdStorage(product , numberof, quantity, price );
     } 
    });

    $('.quantity').html(($( ".quantity-slider" ).slider("value"))*5);
});
  
  

我有一个范围滑块,我将其值存储在对象中,这些对象被推入数组并存储在 localStorage 中。这是我想将它们推送到 localStorage 中的格式。

[

{ "product":"Twitter Followers","numberof":"Number of Followers: ","quantity":"5,500","price":"550" },
{ "product":"Facebook Page Likes","numberof":"Number of Likes: ","quantity":"1,000","price":"100" },
{ "product":"Instagram Followers","numberof":"Number of Followers: ","quantity":"2,500","price":"250" },
{ "..":"..","..":"..: ","..":"..","..":".." }

]

每次范围滑块移动时,我都会根据用户尝试购买的产品名称检查新的值,例如“Facebook Page Likes”、“Instagram Followers”。如果名称相同,我只是尝试更新本地存储中数量和价格的值。如果本地存储中不存在该名称,我想将其添加到其中。问题是一切都变得一团糟。当我移动滑块时,它实际上会更新本地存储中的值,当它可以在“Facebook Page Likes”页面上说时,但是当我移动滑块时转到另一个(例如“Twitter Followers”)时,带有值的新对象会覆盖旧的,不会添加到以前的。

这是我在浏览页面并运行滑块时在控制台中得到的示例。每次在本地存储中只保留一个对象,而前一个被完全删除。

存储 {prodbank:"{"product":"Facebook Page Likes","numberof":"赞数:","quantity":"1,000","price":"100"}",长度: 1}

我希望它添加并保留旧对象的值,而不是完全删除它。例如,这就是我想要的。在这里,您可以看到第二个具有 twitter 关注者值的对象存在,它已添加到存储中

存储 {prodbank: "{"product":"Facebook Page Likes","numberof":"赞数:","quantity":"1,000","price":"100"}", "{"product":"Twitter 关注者","numberof":"关注者数量:","quantity":"2,000","price":"200"}",长度:2}

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    如果您希望添加而不是覆盖新值,则需要使用+= 而不是=。那是因为+= 会将右侧值添加到左侧值,而= 会将左侧值替换为右侧值

    试试:

    itemsArray[i].quantity  +=  quantity; 
    itemsArray[i].price     +=  price;
    

    如果我对你想要达到的目标的理解是正确的。

    【讨论】:

    • 谢谢,但这不是我要找的。每个对象的值都被正确计算(它们不是问题)。这与计算无关。我想添加本地存储中不存在的每个新对象,但它没有发生。实际上它已被添加,但它覆盖了旧的。我用最后四段编辑了这个问题,因为它具有误导性
    猜你喜欢
    • 2012-08-23
    • 2017-06-06
    • 1970-01-01
    • 2022-08-19
    • 2021-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-02
    相关资源
    最近更新 更多