Skip to content

Instantly share code, notes, and snippets.

@cferdinandi
Created May 21, 2018 21:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cferdinandi/4c628c527200b9aae81dd10ca42c090a to your computer and use it in GitHub Desktop.
Save cferdinandi/4c628c527200b9aae81dd10ca42c090a to your computer and use it in GitHub Desktop.
var addToLocalStorageArray = function (name, value) {
// Get the existing data
var existing = localStorage.getItem(name);
// If no existing data, create an array
// Otherwise, convert the localStorage string to an array
existing = existing ? existing.split(',') : [];
// Add new data to localStorage Array
existing.push(value);
// Save back to localStorage
localStorage.setItem(name, existing.toString());
};
var addToLocalStorageObject = function (name, key, value) {
// Get the existing data
var existing = localStorage.getItem(name);
// If no existing data, create an array
// Otherwise, convert the localStorage string to an array
existing = existing ? JSON.parse(existing) : {};
// Add new data to localStorage Array
existing[key] = value;
// Save back to localStorage
localStorage.setItem(name, JSON.stringify(existing));
};
@anton-mladenov
Copy link

On line 24 you can change the last word "array" to an "object" since that's what this function is about, right.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment