2016-03-25 36 views
1

添加在本地存儲新對象將覆蓋舊的


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

enter image description here

我有一個範圍滑塊哪些值我存儲被壓入陣列,並存儲在內部的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" }, 
{ "..":"..","..":"..: ","..":"..","..":".." } 

]

每次範圍滑塊移動我由產品的用戶是在試圖購買如「臉譜頁面喜歡」的名稱檢查新到來的價值觀,」 Instagram的追隨者「。如果名稱相同,我只是試圖更新本地存儲中數量和價格的值。如果名稱不存在於本地存儲中,我想將其添加到裏面。問題是一切都變成了一團糟。當我移動滑塊時,它實際上會更新本地存儲器中的值,當它在「Facebook頁面贊」頁面上說時,但當我移動滑塊時,另一個(例如「Twitter關注者」)帶有值覆蓋的新對象舊的,並沒有添加到以前的。

這是我在瀏覽頁面並運行滑塊時在控制檯中得到的示例。每次只有一個對象被保存在本地存儲器中,並且前一個被完全刪除。

存儲{prodbank:「{」product「:」Facebook Page Likes「,」numberof「:」Number of Likes:「,」quantity「:」1,000「,」price「:」100「}」 :1}

我希望它添加並保留舊對象的值不要完全刪除它。這就是我想要的例子。在這裏,您可以看到第二個包含Twitter追隨者值的對象存在並且它被添加到存儲器中

Storage {prodbank:「{」product「:」Facebook Page Likes「,」numberof「:」贊成數:數量「:」1,000「,」價格「:」100「}」, 「{」product「:」Twitter追隨者「,」numberof「:」追隨者人數:「,」數量「:」2,000「 ,「price」:「200」}「,length:2}

回答

2

如果您希望添加新值而不是覆蓋,則需要使用+=而不是=。這是因爲+=將右邊的值添加到左手值,而=將與右邊的值代替左邊的值

嘗試:

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

也就是說,如果我的理解你試圖達到的是正確的。

+0

謝謝,但這不是我要找的。每個對象的值都可以正確計算(它們不是問題)。這不是計算。我想添加本地存儲中不存在的每個新對象,但不會發生。其實它已被添加,但它會覆蓋舊的。我用最後四個段落編輯了這個問題,因爲這是誤導 –

相關問題