-2

For example, I have 2 arrays. One is myFavCars and one is myDislikedCars. How can I store them in local storage something like:

localStorage.setItem('myPreferences', { myFavCars: [], myDislikedCars: [] })

Is possible to store 2 different arrays in one item on local storage?

Maik Lowrey
  • 15,957
  • 6
  • 40
  • 79
andreiw
  • 11
  • 1

1 Answers1

1

Yes you can with JSON.stringify to parse it to string, but then you will get a string you need to parse it back to Object.

localStorage.setItem('myPreferences',JSON.stringify({ myFavCars: [], myDislikedCars: []}))

And can read it with this:

const objt = JSON.parse(localStorage.getItem('myPreferences'))
Woohaik
  • 1,084
  • 1
  • 6
  • 24
  • yeah, but my problem is that 2 arrays are updating so when I fav a new car I should set in local storage, but this: `localStorage.setItem('myPreferences',JSON.stringify({ myFavCars: [favCarsLocalArray] }))` will replace the viewed one :( – andreiw Nov 15 '21 at 10:15