How to add text to an object in the browser localSorage instead of replacing it

question:
how do I add text to an object in the browser localSorage instead of replacing it?

Mar.22,2021

first take it out and merge it in set


localStorage.setItem ("name", "caibin") / / Store the variable with the name caibin
localStorage.getItem ("name") / / caibin, reads the value of the variable named name in the localStorage object
localStorage.removeItem ("name"); / / Delete name
localStorage.clear () / clear the cache


localStorage.setItem("name",localStorage.getItem("name") + "test")
< hr class= "answer"
const appendTextForStorage = storage => (key, value)  => 
   storage.setItem(key, (storage.getItem(key) || '') + value)

const appendTextForLocalStorage = appendTextForStorage(window.localStorage)


appendTextForLocalStorage('hello', 'world')
appendTextForLocalStorage('hello', 'world2')

// test
console.log(window.localStorage.getItem('hello') === 'worldworld2') // true

I hope my answer will be helpful to you.

Menu