每次都会删除localStorage值,并为同一个键保存一个新的更大字符串。
   
  
  
  
    class PersistentArray  {
    constructor(name) {
      this.name = name;
      this.length = +localStorage.getItem(name) || 0;
    }
    push(value) {
     set(this.length, value);
    }
    set(index, value) {
      if(index >= this.length)
        localStorage.setItem(this.name, this.length = index + 1);
      localStorage.setItem(this.name + index, JSON.stringify(value));
    }
    get(index) {
      return JSON.parse(localStorage.getItem(this.name + index));
    }
    *[Symbol.iterator] () {
       for(let i = 0; i < this.length; i++)
           yield this.get(i);
    }
 }
  
   这样,您可以轻松地将值推送到:
  
    const pages = new PersistentArray("pages");
  // ... somewhen
  pages.push({ value: "whatever" });
  
   当所有数据都存在时:
  
    // Turn into a real in memoy array:
  const result = [...pages];
  // Dynamically load:
  for(const page of pages) 
    console.log(page);