Sobes.tech
Junior

JavaScript obyekti necə localStorage-ə yadda saxlamaq olar?

sobes.tech Süni İntellekt

AI-dan cavab

Obyektin yadda saxlanması üçün JSON.stringify() istifadə edərək obyekt serializə edilir və alınarkən JSON.parse() istifadə edilərək string yenidən obyektə çevrilir.

// Nümunə obyekt
const userSettings = {
  theme: 'dark',
  fontSize: 16,
  notifications: true
};

// Obyekti localStorage-ə yadda saxla
localStorage.setItem('userSettings', JSON.stringify(userSettings));

// Obyekti localStorage-dən əldə et
const storedSettings = localStorage.getItem('userSettings');

// Stringi yenidən obyektə deserializə et
if (storedSettings) {
  const parsedSettings = JSON.parse(storedSettings);
  console.log(parsedSettings);
  // Nəticə: { theme: 'dark', fontSize: 16, notifications: true }
}