JS LocalStorage 导出导入工具

let genStorageSyncCode = () => {
  const kvList = []
  for (let i = 0; i < localStorage.length; i++) {
    const k = localStorage.key(i)
    const v = localStorage.getItem(k)
    kvList.push({
      k, v
    })
  }
  const code = `${kvList.map((item) => {
    return `localStorage.setItem('${item.k}','${item.v}')`
  }).join(';')}`
  return code
}

console.log(genStorageSyncCode())

直接导出代码,复制到另外一个页面控制台console粘贴回车即可进行storage同步

荣耀属于 yinode
https://yinode.tech/post/202110/js-localstorage-%E5%AF%BC%E5%87%BA%E5%AF%BC%E5%85%A5%E5%B7%A5%E5%85%B7/

你可能感兴趣的:(笔记,javascript,前端,开发语言)