0

I am using the below code to add a key-value pair to the existing localStorage object.

But it is not adding a new pair

  var students = []
  var key = 'shop/elasticCache/sku$$' + this.product.parentSku
  var student1 = { books: 1 }
  students.push(student1)
  var old = []
  old = localStorage.getItem(key)
  localStorage.setItem(key, JSON.stringify(old) + JSON.stringify(students))
  console.log(JSON.parse(key)))
ksav
  • 20,015
  • 6
  • 46
  • 66
  • 2
    `old + JSON.stringify(students)` is incorrect. `old` is a string, and you're appending another string to it - that is going to result in invalid JSON. You should `JSON.parse(old)` first to turn it into an array, then add the elements and `JSON.stringify` the new value. – VLAZ Jul 21 '20 at 06:28
  • Thanks for response, please add an answer –  Jul 21 '20 at 06:29
  • No sir, that did't –  Jul 21 '20 at 06:43

0 Answers0