简体   繁体   English

如何附加到 HTML5 localStorage?

[英]How to append to HTML5 localStorage?

I do a我做一个

localStorage.setItem('oldData', $i("textbox").value);

to set the value of key oldData to the value in a textbox.将键oldData的值设置为文本框中的值。

The next time something is entered to the textbox, I want to append to the already existing oldData .下次在文本框中输入内容时,我想附加到已经存在的oldData

So, if I enter apples and then oranges in my textbox, my localStorage key oldData should look like this:因此,如果我在文本框中输入applesoranges ,我的 localStorage 键oldData应如下所示:

Initially:最初:

Key      |   Value
---------------------------
oldData  |  apples

Now:现在:

oldData  |  apples oranges

How can I append?我怎样才能追加? Is there an append method provided?是否提供了append方法?

Or do I need to read the data first, append in javascript and then write back?还是我需要先读取数据,在 javascript 中追加然后写回?

There's no append function.没有append功能。 It's not hard to write one though:不过写一个也不难:

function appendToStorage(name, data){
    var old = localStorage.getItem(name);
    if(old === null) old = "";
    localStorage.setItem(name, old + data);
}

appendToStorage('oldData', $i("textbox").value);

Note: It makes more sense to define a function append on the localStorage object.注意:在localStorage对象上定义一个函数append更有意义。 However , because localStorage has a setter , this is not possible.但是,因为localStorage有一个setter ,这是不可能的。 If you were trying to define a function using localStorage.append = ... , the localStorage object will interpret this attempt as "Save an object called append in the Storage object" .如果您尝试使用localStorage.append = ...定义函数,则localStorage对象会将这种尝试解释为“在存储对象中保存名为append的对象”

It is not a good solution but it works and is performative.这不是一个好的解决方案,但它有效并且具有执行力。

localStorage.setItem("fruit", "Apples"); 

localStorage.setItem("fruit", localStorage.getItem("fruit") + "Orange");

I found this here :我在这里找到了这个:

interface Storage {
  readonly attribute unsigned long length;
  DOMString? key(unsigned long index);
  getter DOMString getItem(DOMString key);
  setter creator void setItem(DOMString key, DOMString value);
  deleter void removeItem(DOMString key);
  void clear();
};

Seems like it only has getItem,setItem,removeItem,clear,key and length.似乎它只有 getItem、setItem、removeItem、clear、key 和 length。

Technically, you could do this by assigning index numbers to the key name.从技术上讲,您可以通过为键名分配索引号来做到这一点。
If you had a HUGE string of data to save, you could save yourself reading and writing the entire string history to the storage and just advance the index name number with the new data.如果你有一大串数据要保存,你可以省去自己读取和写入整个字符串历史到存储的工作,只需用新数据推进索引名称编号。 Something like this.像这样的东西。

function AppendToLocalStorageV(name, value) {
    let n = 0;
    while (localStorage.getItem(name + n)) {
        n++;
    }
    localStorage.setItem(name + n, value); //add item at first available index number
}

This would add the first available index number to the end of the key value, thereby creating a new entry.这会将第一个可用索引号添加到键值的末尾,从而创建一个新条目。 I would also keep track of the current index number to avoid reading all of the entries, or get the localStorage.length variable.我还会跟踪当前的索引号以避免读取所有条目,或者获取 localStorage.length 变量。 The table would look like this桌子看起来像这样

在此处输入图片说明

Then you could retrieve the key array like so.然后你可以像这样检索键数组。

function GetLocalStorageV(name) {
    let n = 0;
    let data = [];
    while (localStorage.getItem(name + n)) {
        data.push(localStorage.getItem(name + n++));
    }
    return data;
}

I added V to the end of the functions to distinguish that we are building vertical arrays in the storage grid.我在函数末尾添加了 V 以区分我们正在存储网格中构建垂直阵列。

Now we can simply do this to append and recall the data by it's name.现在我们可以简单地通过它的名称来附加和调用数据。

AppendToLocalStorageV("oldData", "apples");    
AppendToLocalStorageV("oldData", "oranges");
AppendToLocalStorageV("oldData", "bananas");
var oldData = GetLocalStorageV("oldData");  //returns an array with [apples,oranges,bananas]

You could then access your data just like an array.然后您可以像访问数组一样访问您的数据。 Get and set values at certain indexes, pop from the end of the array etc. Keep in mind, this creates a new entry every time you append.在某些索引处获取和设置值,从数组的末尾弹出等。请记住,每次追加时都会创建一个新条目。 Rebuilding the index numbers could get expensive if you had to reorder the list as well.如果您还必须对列表重新排序,则重建索引号的成本可能会很高。 Also, I don't know how many keys you can safely generate before you slow down the browser or hit a [max_keys] limit.此外,我不知道在降低浏览器速度或达到 [max_keys] 限制之前,您可以安全地生成多少个密钥。 But at least you don't have to constantly read and write the entire list of a HUGE string of data every time as it's only writing the new data.但至少你不必每次都不断地读取和写入一个巨大的数据字符串的整个列表,因为它只是在写入新数据。 I've never tested this with any large amounts of entries, so I'd be careful with this approach.我从来没有用任何大量的条目测试过这个,所以我会小心这种方法。

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

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