想将数据推送到一个数组,但是新数据替换了旧数据

Want to push data to an array, but the new data replaces the old data

当我想向localStorage数组推送数据时,新数据替换了旧数据。我该如何解决?

function addNametext(e) {

    const arrData = inpot1.value

    let text1;
    if (localStorage.getItem("text1") === null) {

        text1 = [];

    } else {

        text1 = JSON.parse(localStorage.getItem("text1"));

    }
    text1.push(arrData);

    localStorage.setItem("name", JSON.stringify(text1));

    alert("SAVED");

    e.preventDefault();
}

您正在设置名称为 name 的密钥,但使用 text1 进行提取。您需要更正

text1 = JSON.parse(localStorage.getItem("text1")); //Notice the key

localStorage.setItem("name", JSON.stringify(text1)); //Notice the key here