简体   繁体   English

遍历数组以将字符串值存储在 localStorage 中的对象中并检索它们

[英]Looping through array to store string values in objects in localStorage and retrieving them

I'm building a web app that lets the user curate a double-feature film showing.我正在构建一个 web 应用程序,让用户可以策划一部双功能电影放映。 The user enters a title, a blurb, and two film titles.用户输入标题、简介和两个电影标题。 On a submit button a function is called that displays the user-submitted title, user-submitted blurb, and makes two separate API calls to retrieve movie information on each respective feature.在提交按钮上调用 function 以显示用户提交的标题、用户提交的简介,并进行两次单独的 API 调用以检索有关每个功能的电影信息。

I'm trying to establish something of a favorites functionality that utilizes localStorage.我正在尝试建立一些利用 localStorage 的收藏夹功能。 Conceptualizing the solution, let alone implementing it may be my first mistake, so I'm open to alternative suggestions, but I believe the best way to do this is to capture each search field value (title, blurb, movie_1, movie_2), store these four string values in an object and then push that object to an array, placing each object into localStorage and then getting each object from localStorage later on with a button click.概念化解决方案,更不用说实施它可能是我的第一个错误,所以我愿意接受其他建议,但我相信最好的方法是捕获每个搜索字段值(标题、简介、电影_1、电影_2)、存储这四个字符串值在 object 中,然后将该 object 推入一个数组,将每个 object 放入 localStorage,然后通过单击按钮从 localStorage 获取每个 object。

I'm able to capture these items, store them in localStorage and dynamically generate buttons that when clicked populates the four search field values back into the respective search fields, allowing the user to click the submit button again which runs the api calls and displays all of the content (again: title, blurb, movie_1, movie_2).我能够捕获这些项目,将它们存储在 localStorage 中并动态生成按钮,当单击这些按钮时会将四个搜索字段值填充回各自的搜索字段,允许用户再次单击提交按钮运行 api 调用并显示所有内容(再次:标题、简介、movie_1、movie_2)。

My problem is looping through the objects and grabbing the different search field data values.我的问题是遍历对象并获取不同的搜索字段数据值。 I suppose the problem is in assigning a name or key to the different objects that I'm looping through in the array and then accessing the correct values from the appropriate button through localStorage.我想问题在于为我在数组中循环的不同对象分配名称或键,然后通过 localStorage 从适当的按钮访问正确的值。 I seem to be setting the same localStorage object (or rewriting it) and accessing it over and over, as opposed to setting a new localStorage object and getItem'ing the right one.我似乎正在设置相同的 localStorage object(或重写它)并一遍又一遍地访问它,而不是设置一个新的 localStorage object 并获取正确的 getItem。

I'll provide some code snippets below, but it might be easier to peruse my GitHub repo: https://github.com/mchellagnarls/double_feature我将在下面提供一些代码片段,但仔细阅读我的 GitHub 存储库可能更容易: https://github.com/mchellagnarls/double_feature

If you look at the repo, latest code is found in index_test.html and app_test.js, whereas a previous version without any of the broken favorite functionality is found in index.html and app.js.如果你查看 repo,最新的代码在 index_test.html 和 app_test.js 中,而没有任何损坏的收藏夹功能的以前版本在 index.html 和 app.js 中找到。

Some code snippets:一些代码片段:

// logic to capture search field values and to eventually display them as buttons
  // empty array
  var dfArray = [];

  // object to hold each of the string values to populate the search fields
  var doubleFeature = {
    feature_1: movie_1,
    feature_2: movie_2,
    DFTitle: title,
    DFBlurb: blurb
  }

  dfArray.push(doubleFeature);

  for (var i = 0; i < dfArray.length; i++) {

    localStorage.setItem("df", JSON.stringify(dfArray[i]));

    var button = $("<button>");
    button.addClass("df-favorite-button");
    button.text(title);

    $("#buttons-view").append(button);
  }

$(document).on("click", ".df-favorite-button", function() {
  event.preventDefault();

  var savedDF = JSON.parse(localStorage.getItem("df"));

  $("#movie-input-1").val(savedDF.feature_1);
  $("#movie-input-2").val(savedDF.feature_2);
  $("#df-title-input").val(savedDF.DFTitle);
  $("#df-blurb-input").val(savedDF.DFBlurb);
})

Thanks for any help.谢谢你的帮助。 I'm learning web development and I may be overcomplicating things or missing out on an easier way to think about it and/or solve it.我正在学习 web 开发,我可能会使事情过于复杂,或者错过了一种更简单的思考和/或解决问题的方法。

Instead of this loop:而不是这个循环:

  for (var i = 0; i < dfArray.length; i++) {

    localStorage.setItem("df", JSON.stringify(dfArray[i]));

    var button = $("<button>");
    button.addClass("df-favorite-button");
    button.text(title);

    $("#buttons-view").append(button);
  }

I would place the whole array into local storage我会将整个阵列放入本地存储

localStorage.setItem("df", JSON.stringify(dfArray));

Also then you have to decide which object to get from array in click function然后你必须决定从数组中获取哪个 object 单击 function

$(document).on("click", ".df-favorite-button", function() {
  event.preventDefault();

  var id = -1; // which one

  var savedDF = (JSON.parse(localStorage.getItem("df")) || []).find(pr => pr.id === id);

  $("#movie-input-1").val(savedDF.feature_1);
  $("#movie-input-2").val(savedDF.feature_2);
  $("#df-title-input").val(savedDF.DFTitle);
  $("#df-blurb-input").val(savedDF.DFBlurb);
})

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

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