简体   繁体   English

如何从本地存储中获取数组并将其显示在 HTML

[英]How do I get an array from local storage and display it in a HTML

I am stuck.我被困住了。 I am using localStorage for the first time as a beginner in JS, but I am failing to get the saved data and display it on the front end.作为 JS 的初学者,我第一次使用 localStorage,但我无法获取保存的数据并将其显示在前端。 When I console.log I can see the data in the console, but how to get it not the page to display.当我使用 console.log 时,我可以在控制台中看到数据,但如何获取它不是要显示的页面。 I am really looking for basics, whether I should create a function for both (there are two) and loop through it.我真的在寻找基础知识,是否应该为两者(有两个)创建一个 function 并循环遍历它。

I am really out of my depth and would appreciate any tips.我真的超出了我的深度,并将不胜感激任何提示。

This is my JS which I have been working on bit by bit:这是我的 JS,我一直在一点一点地工作:

//Array to hold statistics of number of pages read per sitting  over a period of time
let myArray = [];

//Function to calculate number of pages read
let calculate = () => {
  var start = document.getElementById("number1").value;
  var end = document.getElementById("number2").value;
  //document.getElementById("number1").value = ""; // make START empty
  //document.getElementById("number2").value = ""; //make END empty
  let totalNumber = end - start; 

  myArray.push(totalNumber); //Push total number into array
  //Store myArray in localStorage  and retrieve
  localStorage.setItem("items", JSON.stringify(myArray));
  localStorage.getItem("items");

  //How do I display this in array?



  document.getElementById("congrats").textContent =
    "Congrats you just revised " + totalNumber + "  pages!";//display message
  document.getElementById("rightpane1").textContent = myArray;//push array into HTML

  displayArray();//Call display array


};

//Function to display myARRAY broken down to a new line
const displayArray = ()=> {
  let displayedNumbers='';
  for (i = 0; i < myArray.length; i++){

       displayedNumbers += myArray[i] + "\n";
       //Calculate myArray and give sum total of numbers
       var sum = myArray.reduce(function (accumulator, currentValue) {
        return accumulator + currentValue;
      }, 0);

      //Store sum in localStorage and retrieve
       localStorage.setItem("items2", JSON.stringify(sum));
       localStorage.getItem("items2");
       //How do I display this in SUM
  };  
  document.getElementById("rightpane1").textContent = displayedNumbers;//Append myArray into HTML

  //Add sum of array and display it in HTML
  document.getElementById("rightpane2").textContent = 'Total ' + sum;  

}

//Get myArray from local storage

const displayArrayData = () => (){
   let myArrayObject = JSON.parse(localStorage.getItem("items"));
}

//get SUM from local storange
const displaySumData = () => {
   let mySumObject = JSON.parse(localStorage.getItem("items2"));

}

You have done it right in here:您已经在这里完成了:

document.getElementById("rightpane1").textContent = displayedNumbers;

Because the variable displayedNumbers is defined outside the for loop, so it is visible outside it.因为变量displayedNumbers是在for循环之外定义的,所以在它外面是可见的。

In order to get the sum value, you have to define a variable outside the for loop aswell, and then increment it on your reduce function:为了获得总和值,您还必须在 for 循环之外定义一个变量,然后在您的 reduce function 上增加它:

const displayArray = ()=> {
  let displayedNumbers='';
  var sum = 0;
  for (i = 0; i < myArray.length; i++){

       displayedNumbers += myArray[i] + "\n";
       //Calculate myArray and give sum total of numbers
       sum += myArray.reduce(function (accumulator, currentValue) {
         return accumulator + currentValue;
       }, 0);

      //Store sum in localStorage and retrieve
       localStorage.setItem("items2", JSON.stringify(sum));
       localStorage.getItem("items2");
       //How do I display this in SUM
  };  
  document.getElementById("rightpane1").textContent = displayedNumbers;

  //Add sum of array and display it in HTML
  document.getElementById("rightpane2").textContent = 'Total ' + sum;  

}

I've updated your displayArray function below:我在下面更新了您的displayArray function:

//Function to display myARRAY broken down to a new line
const displayArray = ()=> {
  let displayedNumbers=myArray.join('');

  //Calculate myArray and give sum total of numbers
  var sum = myArray.reduce(function (accumulator, currentValue) {
     return accumulator + currentValue;
  }, 0);
  //Store sum in localStorage and retrieve
  localStorage.setItem("items2", JSON.stringify(sum));

  document.getElementById("rightpane1").textContent = displayedNumbers;//Append myArray into HTML

  //Add sum of array and display it in HTML
  document.getElementById("rightpane2").textContent = 'Total ' + sum;  

}

The problem with your original code is that, your sum variable is inside the for loop which concatenates the numbers into a string.原始代码的问题在于,您的sum变量位于将数字连接成字符串的for循环内。 Also, I've made use of the join function of array instead of looping through it.另外,我使用了数组的连接function 而不是循环遍历它。

Although I'm not sure if this is where your problem is based on what you said虽然我不确定这是否是您的问题所在

...but I am failing to get the saved data and display it on the front end.

Because sum (in the above code) is not derived from the localStorage at all.因为sum (在上面的代码中)根本不是从localStorage派生的。

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

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