Skip to content
Advertisement

Single function for saving into localStorage?

How can we save and retain localStorage objects rather than creating multiple functions ? The first localStorage object get replaced with the new save. So to avoid that I have created a new function called saveLocalStorageDataTwo which is working. But how can we avoid creating multiple functions for saving data into the localStorage ? Is there any way ? Could some please advise ?

/* localStorage.js */

JavaScript

// Saving to localStorage:

JavaScript

enter image description here

Advertisement

Answer

  • You simply don’t use any strict params like {autoId, quoteId, taskId} just pass any arbitrary data.
  • Don’t call something saveLocalStorageData if that’s actually not what that function does.

Instead:

JavaScript
JavaScript

jsFiddle playground

or in the last example instead of an Array you could have used an Object. It all depends on the needs.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement