简体   繁体   English

如何更新本地存储中数组内的id

[英]how to update the id's inside an array in localstorage

I have a bunch of li with id's that is related with the id's inside the array in localstorage, when I delete an li the element from the array is deleted as well, but if I recharge my navigator the id's of my li's is updating but in localstorage is not update, how can i update the ids into the localstorage and make match with the li id's我有一堆 li 的 id 与 localstorage 中数组内的 id 相关,当我删除一个 li 时,数组中的元素也会被删除,但是如果我为导航器充电,我的 li 的 id 正在更新,但在localstorage 未更新,如何将 id 更新到 localstorage 并与 li id 匹配

    const listaTweets = document.getElementById('lista-tweets');
let li;
let comment;

evenListeners();


function evenListeners() {
    document.querySelector('#formulario').addEventListener('submit', addTweet);
    listaTweets.addEventListener('click', deleteComment);
    document.addEventListener('DOMContentLoaded', localStorageDone)
}

function addTweet(e) {
    e.preventDefault();
    comment = document.getElementById('tweet').value;
    if(comment) {
        createLi(comment)
        const liId = li.id
        addCommentLocalStorage(comment, liId)
    }
}

function createLi(comment) {
    const deleteButton = document.createElement('a');
    deleteButton.classList = 'delete-comment';
    deleteButton.innerText = 'x';

    li = document.createElement('li');
    li.innerText = comment;
    li.appendChild(deleteButton);

    listaTweets.appendChild(li);

    if (li) {
        for (let i = 0; i < listaTweets.children.length; i++) {
            li.setAttribute('id', 'tweet__number' + i)
        }
    }
}

function deleteComment(e) {
    e.preventDefault();
    li = e.target.parentElement;
    if(e.target.className === 'delete-comment') {
        li.remove();
        deleteCommentLocalStorage()
    }
}

function addCommentLocalStorage(comment, liId) {
    let arrayComments;
    let idComment = liId
    arrayComments = getFromLocalStorage();

    arrayComments.length === 0 ? idComment = liId : (arrayComments[arrayComments.length - 1].idComment)

    let object = {
        id: idComment,
        com: comment
    }
    
    arrayComments.push(object)
    localStorage.setItem('comments', JSON.stringify(arrayComments))
}

function getFromLocalStorage() {
    let arrayComments;
    if(localStorage.getItem('comments') === null) {
        arrayComments = []
    } else {
        arrayComments = JSON.parse(localStorage.getItem('comments'))
    }
    
    return arrayComments
}

function localStorageDone() {
    let arrayComments;
    arrayComments = getFromLocalStorage();
    arrayComments.forEach(function(comment){
        createLi(comment.com)
    })
}

function deleteCommentLocalStorage() {
    let arrayComments = getFromLocalStorage();

    arrayComments.forEach(function(comment){
       if(comment.id === li.id) {
            let i = arrayComments.indexOf(comment);
            arrayComments.splice(i, 1);
        }
        
        localStorage.setItem('comments', JSON.stringify(arrayComments));
    })
}

I found a typo.我发现一个错字。

// before 
arrayComments.length === 0 ? idComment = liId : (arrayComments[arrayComments.length - 1].idComment)
// after
arrayComments.length === 0 ? idComment = liId : (arrayComments[arrayComments.length - 1].id)

However, after correcting the typo, it seems that there is a problem that the id of listaTweets and the id of arrayComments do not match each other when inserting/removing items.但是,在更正错字之后,似乎存在插入/删除项目时listaTweets 的id 和arrayComments 的id 不匹配的问题。 I think you should check this more.我认为你应该多检查一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM