Local Storage在调用时未能移除数组中的对象,使用了localStorage.remove方法。

huangapple go评论64阅读模式
英文:

Local Storage not removing object in array when called, using the localStorage.remove method

问题

I'm building a movie watch list that stores movies in my local storage, and removes them. Here are the two CodePens for my code, one which searches & adds:

https://codepen.io/vanessalearnsjavascript/pen/vYVJPgm?editors=0011

And the other which is the "Watch List" page that removes:

https://codepen.io/vanessalearnsjavascript/pen/YzJEQEW?editors=0011

removeMovieButton.addEventListener("click", function() {
  watchList.splice(watchList.indexOf(movie), 1);
  movieDiv.parentNode.removeChild(movieDiv);
  localStorage.removeItem("movie" + watchList.indexOf(movie));
});

On the WatchList page, I have a "Remove Movie" button rigged up that removes the movie from the array and from the DOM, but the local storage of it does not work. Here is the removeMovie button, with the local storage line that is not executing. I can press the button and it removes the item from the array and the DOM, but when I have the local storage open, it remains.

Can anyone explain how I can fix this and why this is happening? Thanks.

英文:

I'm building a movie watch list that stores movies in my local storage, and removes them. Here are the two CodePens for my code, one which searches & adds:

https://codepen.io/vanessalearnsjavascript/pen/vYVJPgm?editors=0011

And the other which is the "Watch List" page that removes:

https://codepen.io/vanessalearnsjavascript/pen/YzJEQEW?editors=0011

    removeMovieButton.addEventListener("click", function() {
      watchList.splice(watchList.indexOf(movie), 1);
      movieDiv.parentNode.removeChild(movieDiv);
      localStorage.removeItem("movie" + watchList.indexOf(movie));
    });

On the WatchList page, I have a "Remove Movie" button rigged up that removes the movie from the array and from the DOM, but the local storage of it does not work. Here is the removeMovie button, with the local storage line that is not executing. I can press the button and it removes the item from the array and the DOM, but when I have the local storage open, it remains.

Can anyone explain how I can fix this and why this is happening? Thanks.

答案1

得分: 1

你只需要更改执行顺序,所以将这部分代码替换为:

 removeMovieButton.addEventListener("click", function() {
      localStorage.removeItem("movie" + watchList.indexOf(movie)); // < ---- 2
      movieDiv.parentNode.removeChild(movieDiv);
      watchList.splice(watchList.indexOf(movie), 1);// < ---- 1
    });

Explanation: 你首先要从数组中删除电影,然后获取了一个已经被删除的项目的错误索引来从localStorage中删除它,这就是为什么它对你不起作用的原因。

英文:

you have just to change the execution orders, so replace this

 removeMovieButton.addEventListener(&quot;click&quot;, function() {
      watchList.splice(watchList.indexOf(movie), 1); // &lt;-------- 1
      movieDiv.parentNode.removeChild(movieDiv);
      localStorage.removeItem(&quot;movie&quot; + watchList.indexOf(movie)); // &lt;------ 2
    });

by

 removeMovieButton.addEventListener(&quot;click&quot;, function() {
      localStorage.removeItem(&quot;movie&quot; + watchList.indexOf(movie)); // &lt; ---- 2
      movieDiv.parentNode.removeChild(movieDiv);
      watchList.splice(watchList.indexOf(movie), 1);// &lt; ---- 1
    });

Explanation: you are deleting the movie from the array then you are getting a wrong index of the "already" deleted item to remove it from localStorage, that's why it did not work for you .

答案2

得分: 1

为什么它不起作用?你移除了项目,然后又试图再次找到它。在移除后你还能找到它吗?

removeMovieButton.addEventListener("click", function() {
      watchList.splice(watchList.indexOf(movie), 1); <-- 你移除了该条目
      movieDiv.parentNode.removeChild(movieDiv);
      localStorage.removeItem("movie" + watchList.indexOf(movie)); <-- 你又试图寻找它
    });

现在你可以将索引存储到变量中,但是当你移除该索引时,所有其他条目会向下移动一个位置。这意味着数组中的movie4现在在索引3处,而不是4。所以你需要调整所有的本地存储键。

只需存储整个数组!

watchList.splice(watchList.indexOf(movie), 1);
localStorage.setItem('watchList', JSON.stringify(watchList));

当你需要观影列表时,读取它

watchList = JSON.parse(localStorage.getItem('watchList') || '[]');
英文:

Why does it not work? You remove the item and then you try to find that item once again. Are you going to find it after it is removed?

removeMovieButton.addEventListener(&quot;click&quot;, function() {
      watchList.splice(watchList.indexOf(movie), 1); &lt;-- You remove the entry
      movieDiv.parentNode.removeChild(movieDiv);
      localStorage.removeItem(&quot;movie&quot; + watchList.indexOf(movie)); &lt;-- you look for it again
    });

Now you can store the index into variable, but when you remove the index, all of the other entries move down one. So that means movie4 in the array would be at index 3, not 4. So you would have to adjust all of your local storage keys.

Just store the array!

watchList.splice(watchList.indexOf(movie), 1);
localStorage.setItem(&#39;watchList&#39;, JSON.stringify(watchList));

when you need the watch list, read it

watchList = JSON.parse(localStorage.getItem(&#39;watchList&#39;) || &#39;[]&#39;);

huangapple
  • 本文由 发表于 2023年5月18日 03:22:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76275557.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定