Junior
JavaScript nesnesi nasıl localStorage'a kaydedilir?
sobes.tech yapay zeka
AI'dan gelen yanıt
Öğeyi kaydetmeden önce JSON.stringify() kullanarak nesneyi dizeye serileştirir ve alınırken JSON.parse() kullanarak dizeyi tekrar nesneye dönüştürür.
// Örnek nesne
const userSettings = {
theme: 'dark',
fontSize: 16,
notifications: true
};
// Nesneyi localStorage'a kaydet
localStorage.setItem('userSettings', JSON.stringify(userSettings));
// Nesneyi localStorage'tan al
const storedSettings = localStorage.getItem('userSettings');
// Dizeyi tekrar nesneye deseralize et
if (storedSettings) {
const parsedSettings = JSON.parse(storedSettings);
console.log(parsedSettings);
// Çıktı: { theme: 'dark', fontSize: 16, notifications: true }
}