【问题标题】:I am getting repeated input values as an Output when I am trying to get User Input from Prompt box and showing those input values in an Array当我尝试从提示框中获取用户输入并在数组中显示这些输入值时,我将重复的输入值作为输出
【发布时间】:2022-01-14 18:56:19
【问题描述】:

在示例 - 1 中,我得到了从提示框中输入的所有值。

//示例 - 1

  document.getElementById("btn").onclick = () => {

            let cars = new Array(5);

            for(let get = 0 ; get <= 5 ; get++){

                cars[get] = prompt("Enter Any Values");
            }

            for(let show = 0 ; show <= 5 ; show++ ){

                document.getElementById("data1").innerHTML += `You have Entered : ${cars[show]}<br>`;
            }

            document.getElementById("data2").innerHTML = `The Total Values are : ${cars}`;

        }
<button id="btn">Click Here</button>
    <div id="data1"></div>
    <div id="data2"></div>

我在示例 2 中遇到问题。请告诉我哪里做错了。谢谢你..!

//示例 - 2

 document.getElementById("btn").onclick = () => {

            let cars = new Array(5);

            for(let get of cars){
                cars[get] = prompt("Enter Any Values");
            }
            for(let show of cars){

                document.getElementById("data1").innerHTML += `You have Entered : ${cars[show]}<br>`;
            }

            document.getElementById("data2").innerHTML = `The Total Values are : ${cars}`;

        }
<button id="btn">Click Here</button>
    <div id="data1"></div>
    <div id="data2"></div>

【问题讨论】:

  • 我相信你正在尝试使用for (let show in cars)
  • @HassaanAli,是的,在使用for (let show in cars) 之后,我只得到了最后一个输入值,而且我得到了一个空数组的总值。
  • 让我为你写一个详细的答案。
  • @HassaanAli,非常感谢,先生。它在传统的 for 循环(示例 - 1)中运行良好,但在 for-of 和 for-in 循环中,我遇到了问题。

标签: javascript arrays arrow-functions for-in-loop for-of-loop


【解决方案1】:

for of 用于对象的数组值(而不是它们的索引)。 MDN website.

您将for in 用于索引:

const array1 = ['a', 'b', 'c'];

for (const element of array1) {
  console.log(element);
}

for (const element in array1) {
  console.log(element);
}

现在回答第二个代码中发生的情况:

get 在循环中每次都是undefined,因为cars 数组是空的。 所以没有值。 每次在你的循环中都会发生这种情况:

cars[undefined] = (your prompt answer)

cars[undefined] 的值也被覆盖了

此时cars 是一个对象,其键为undefined,值为YOURLASTENTEREDVALUE。 稍后,for of 循环再次在cars 数组上运行以显示值。现在show 又是循环内的undefined因为cars 仍然是空的。

所以每次你再次做cars[undefined]。但是这次你有一个值,这将是你回答的最后一个值,并且正在显示。

document.getElementById("btn").onclick = () => {

            let cars = new Array(3);

            for(let get of cars){
                console.log(get);
                cars[get] = prompt("Enter Any Values");
            }

            console.log(Object.keys(cars));
            console.log(Object.values(cars));
            
            console.log(cars[undefined]);
            for(let show of cars){

              console.log(show);  document.getElementById("data1").innerHTML += `You have Entered : ${cars[show]}<br>`;
            }

            document.getElementById("data2").innerHTML = `The Total Values are : ${cars}`;

        }
<button id="btn">Click Here</button>
    <div id="data1"></div>
    <div id="data2"></div>

编辑 根据 OP 的要求,一种解决方法:

document.getElementById("btn").onclick = () => {

            let cars = new Array(3).fill(0);
              for(let get in cars){
                cars[get] = prompt("Enter Any Values");
            }

            for(let show in cars){
 document.getElementById("data1").innerHTML += `You have Entered : ${cars[show]}<br>`;
            }

            document.getElementById("data2").innerHTML = `The Total Values are : ${cars}`;

        }
<button id="btn">Click Here</button>
    <div id="data1"></div>
    <div id="data2"></div>

最初使用默认 0s 填充数组,以便可以使用 for in 对其进行迭代,这是一种方法。

【讨论】:

  • 感谢先生分享有关此问题的详细信息。这是否意味着我对第二个示例的方法是错误的?有什么方法可以使用 for-in 或 for-of 循​​环获得正确的输出。
  • 你可以。见编辑。迭代长度为 5 的绝对空数组会给您带来问题。只需填写默认值即可。如果对你有帮助,请点赞
猜你喜欢
  • 1970-01-01
  • 2021-03-23
  • 2020-06-15
  • 1970-01-01
  • 2017-03-27
  • 1970-01-01
  • 2018-03-11
  • 2020-11-30
  • 1970-01-01
相关资源
最近更新 更多