可以't使用新对象更新localStorage

Can't update localStorage with new objects

本文关键字:对象 localStorage 更新 新对象 可以      更新时间:2023-09-26

我正在创建一个非常基本的购物车,将订单存储到localStorage。我现在很难用我的新订单来更新我的数组(作为对象输入)。

$scope.checkout = function (shoppingCart) {
    $('#checkoutModal').modal('show');
    var existingEntries = JSON.parse(localStorage.getItem("session"));
    if (existingEntries == null) existingEntries = new Array();
    var entry = {
        ReferenceNumber: '4444-0' + rand,
        Amount: $scope.subTotal,
        Date: new Date(),
        Voided: false
    };
    localStorage.setItem("entry", JSON.stringify(entry));
    // Save allEntries back to local storage
    existingEntries.push(entry);
    localStorage.setItem("allEntries", JSON.stringify(existingEntries));
};

如果有人能指出我失明的地方,我会很高兴的!

session是有效的localStorage项吗?您似乎应该尝试从allEntries获取existingEntries。