修改localstorage? [英] Modify localstorage?

查看:124
本文介绍了修改localstorage?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用simplecartjs为在线商店供电。它将数据存储在本地存储中,如下所示:

I'm using simplecartjs to power an online store. It stores it's data in local storage, which looks like this:

{"SCI-1":{"quantity":1,"id":"SCI-1","price":20,"name":"Mattamusta -teippi","size":"Tyhjä"},"SCI-3":{"quantity":1,"id":"SCI-3","price":19,"name":"Mohawk .vaimennusmatto 48 x 39cm"},"SCI-5":{"quantity":2,"id":"SCI-5","price":8,"name":"Car Speaker -hajuste","color":"Green Tea"},"SCI-7":{"quantity":1,"id":"SCI-7","price":50,"name":"Asennuspaketti"},"SCI-9":{"quantity":1,"id":"SCI-9","price":30,"name":"Quartz-kalvot","color":"Yellow","size":"50cm x 30cm"},"SCI-11":{"quantity":1,"id":"SCI-11","price":30,"name":"Quartz-kalvot","color":"True Blue","size":"50cm x 30cm"}}

我想在之前添加这一行

And I want to add this line before it's closed.

"SCI-12":{"quantity":1,"id":"SCI-12","price":5,"name":"Toimituskulut"}

然而,产品数量将取决于用户,依此类推 SCI-12 必须与n一起增长项目数量。

However, the amount of products will depend on user, and so on SCI-12 must grow with the number of items.

编辑实际上,它不必使用 SCI-1 ,它可以是任何东西,只要因为它是在物品之后。

EDIT Actually, it doens't have to use SCI-1, it can be anything, as long as it's after the items.

EDIT2
这是我正在尝试的......但没有运气。

EDIT2 Here's what I'm trying... But without luck.

$("#matkahuolto").click(function(){
    var value_object = '"Toimituskulut":{"quantity":1,"id":"Toimituskulut","price":5,"name":"Toimituskulut"}';
    var json_object = JSON.parse(localStorage.your_json); // convert string to object

    json_object["simpleCart_items"] = value_object; // add value

    localStorage.your_json = JSON.stringify(json_object);  // store it again.

    });

    $("#posti").click(function(){
    var json_object = JSON.parse(localStorage.your_json); // convert string to object

    json_object["simpleCart_items"] = value_object; // add value

    localStorage.your_json = JSON.stringify(json_object);  // store it again.

    });

EDIT3 localstorage的屏幕截图。

EDIT3 A screenshot of localstorage.

推荐答案

您需要了解的第一件事是如何将项目存储在 localStorage ,它基本上类似于散列映射/字典,因为项目存储在键值对中。

The first thing you need to understand is how items are stored in localStorage, which is basically like a hashmap/dictionary in that the items are stored in key value pairs.

例如存储字符串 localStorage 你会做类似的事情

For example to store a string localStorage you would do something like

localStorage["myKey"] = "some value";

要检索价值,您只需反向

To retrieve the value you would just to the reverse

var myValue = localStorage["myKey"];

接下来要意识到的是,实际上你只能 存储字符串在localStorage中,如果要存储对象,则需要先将其转换为字符串表示形式。这通常通过将对象转换为 JSON 来完成,这是一种用于将对象表示为字符串的紧凑表示法。要将对象转换为JSON,只需使用 JSON.stringify 方法,并解析使用 JSON.parse 方法

The next thing to realize is that you can in fact only store strings in localStorage, so if you want to store a object you need to first convert it to a string representation. This is usually done by converting the object to JSON which is a compact notation used to represent an object as a string. To convert an object to JSON you just use the JSON.stringify method, and to parse an item you use the JSON.parse method

例如将对象转换为JSON

For example to convert an object to JSON

var someObject = {id: 1, name: 'Test'};

var jsonSomeObject = JSON.stringify(someObject); 
// jsonSomeObject looks like "{"id":1,"name":"test"}"

从JSON读取对象

someObject = JSON.parse(jsonSomeObject);

所以现在假设您的对象包含适当的数据,您需要做的只是 stringify 您的对象并使用某个键将其添加到 localStorage

So now in your case assuming that your object contains the appropriate data all you need to do is stringify your object and add it to localStorage using some key,

var jsonData = JSON.stringify({"SCI-12":{"quantity":1,"id":"SCI-12","price":5,"name":"Toimituskulut"}});

localStorage["aKey"] = jsonData;

当你想稍后访问这些数据时(可能是在回到页面后)

and when you want to access this data later (presumably after coming back to the page)

var mySCI = JSON.parse(localStorage["aKey"])

如果您尝试更新localStorage中的特定条目,那么您只需读取它然后覆盖它,例如

If you are trying to update a specific entry in localStorage then you would just read it in and then overwrite it, for example

var mySCI = JSON.parse(localStorage["aKey"]);
mySCI.SCI-12.quantity = 2;
localStorage["aKey"] = JSON.stringify(mySCI);

请记住,虽然大多数浏览器内置了对JSON的支持,但有些旧版本没有,如果这个问题可以包括Douglass Crockfords JSON-js 脚本以提供支持。

Keep in mind that while most browsers have built in support for JSON some older ones don't, if this is an issue you can include Douglass Crockfords JSON-js script to provide support.

编辑

根据您刚刚发布的屏幕截图,您可以看到这些值存储在localStorage中实际上是simpleCart_items(而不是你的代码中使用的your_json),并且存储的对象有一个存储在密钥SCI-1下的对象(可能还有其他一些存储在SCI下) )。所以您需要做的是以下内容

Based on the screenshot you just posted, you can see that the key under which the values are stored in localStorage is in fact simpleCart_items (and not your_json which are using in your code), and that the object that is being stored has a object stored under the key "SCI-1) (and probably others under "SCI's"). So what you would need to do is something like the following

 $("#matkahuolto").click(function(){
    var value_object = {quantity: 1, id: 1, name: "someName"} ; 
    var jsonObject = JSON.parse(localStorage["simpleCart_items"]);
    json_object["SCI_1"] = value_object; // add value

 localStorage["simpleCart_items"] = JSON.stringify(json_object);  // store it again.

});

$("#posti").click(function(){
 // convert string to object
 var json_object = JSON.parse(localStorage["simpleCart_items"]); 

json_object["SCI-1"] = value_object; // add value

 localStorage["simpleCart_items"] = JSON.stringify(json_object);  // store it again.

});

这篇关于修改localstorage?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆