Created
May 21, 2018 21:27
-
-
Save cferdinandi/4c628c527200b9aae81dd10ca42c090a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
On line 24 you can change the last word "array" to an "object" since that's what this function is about, right.