0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

LocalStorageの簡単な使用方法のサンプル

Last updated at Posted at 2023-02-10

目的

備忘録を兼ねて。
LocalStorageに連想配列をJSONで保存/読み込み、保存のサンプル。
保存/取得にキーを指定する必要があるが、連想配列のキーと識別するため、本文中では当該キーをストレージキーと記述する。

サンプルソース

保存

let json_data={"キー1": "値1", "キー2": "値2", ...};
localStorage.setItem(ストレージキー, JSON.stringify(json_data));

取得

let raw_data=localStorage.getItem(ストレージキー);
if(raw_data==null) return {}; //エラー処理: 当サンプルでは空の連想配列を返していますが、適宜修正してください
let json_data=JSON.parse(raw_data);

ストレージキーで指定した情報の削除

localStorage.removeItem(ストレージキー);

LocalStorageの全削除

localStorage.clear();
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?