Programming/Web

#로컬스토리지 배열에 객체 추가하기 예제

현무랑 2019. 10. 8. 09:00
반응형

add array in localstorage 

 

function addEntry() {
    // Parse any JSON previously stored in allEntries
    var existingEntries = JSON.parse(localStorage.getItem("allEntries"));
    if(existingEntries == null) existingEntries = [];
    var entryTitle = document.getElementById("entryTitle").value;
    var entryText = document.getElementById("entryText").value;
    var entry = {
        "title": entryTitle,
        "text": entryText
    };
    localStorage.setItem("entry", JSON.stringify(entry));
    // Save allEntries back to local storage
    existingEntries.push(entry);
    localStorage.setItem("allEntries", JSON.stringify(existingEntries));
};

 

출처 : https://codeday.me/ko/qa/20190423/389265.html

 

반응형

'Programming > Web' 카테고리의 다른 글

#LocalStorage 예제  (1) 2019.10.11
#2차원 배열을 로컬스토리지 저장하는 예제  (0) 2019.10.07