undefinedfix
Sign in

How can this code be optimized

thnqu edited in Wed, 31 Aug 2022

const { a, b, c, d} = res.data;

localStorage.a = a
localStorage.b = b
localStorage.c = c
localStorage.d = d
3 Replies
Silverfern
commented on Thu, 01 Sep 2022

Maintain a list of key names to cycle through assignments, like this

for (let key of ['a', 'b', 'c', 'd']) {
  localStorage[key] = res.data[key]
}
andremoon17
commented on Thu, 01 Sep 2022

localStorage = res.data

If localstorage is H5 localstorage, use its API to operate

pazhitnov
commented on Thu, 01 Sep 2022
for(let k in res.data){
    localStorage[k] = res.data[k];
}