简体   繁体   English

如何将所有项目保留在数组函数中? 使用Javascript

[英]How would I keep all items inside an array function? Javascript

How would I put all the numbers inputted in the function getNum() , into the finalArr Array , without moving var finalArr = []; 我如何将在函数getNum()输入的所有数字放入finalArr Array ,而无需移动var finalArr = []; outside the function it is in? 它在功能之外? At the moment, no matter how many cycles I run the script, it only displays the very last number I entered. 此刻,无论我运行脚本有多少个周期,它只会显示我输入的最后一个数字。

function getNum () {

     do {    
         number = Number(prompt("Enter a number:"));
     }
     while (number < 0);

     return number;

}

function finalArray () {

    var finalArr = [];
    finalArr.unshift(number);
    alert(finalArr);

    return;
}

do {
     getNum();
     finalArray();
     var no = prompt("Enter n to stop");
}
while (no != "n");

您必须在getNum()方法中调用finalArry()方法,并将其从do循环中删除。

Why don't you use a recursive function instead of a while loop? 为什么不使用递归函数而不是while循环?

Look at the next code: 看下面的代码:

 var array = []; function getNum () { var no = prompt("Enter n to stop"); if (no === "n") { console.log("the final array is"); console.log(array); } else { array.push(no); getNum(); } } getNum(); 

EDIT: I don't understand why you want to declare the Array inside the function, every variable declared inside a function lives just in that scope. 编辑:我不明白为什么要在函数内部声明Array ,在函数内部声明的每个变量都位于该范围内。 But if it is necessary for you, you could pass the Array value to the function, declare the Array inside the function getting the value that you have passed and return the resulting Array , it is a bit tricky but it works: 但是,如果您有必要,可以将Array值传递给函数,在函数内部声明Array ,以获取您传递的值,然后返回生成的Array ,这有点棘手,但是可以使用:

 var array; function finalArray(array, number) { var finalArr = array || []; finalArr.push(number); return finalArr; } function getNum() { var no = prompt("Enter a number. Enter n to stop"); if (no === "n") { console.log("the final array is"); console.log(array); } else { array = finalArray(array, +no); getNum(); } } getNum(); 

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

相关问题 如何将数组中的这些项目移动到数组中的对象中? - How would I move these items inside an array into objects within an array? JavaScript:同时从数组中删除三个项目。 如何跟踪所有已删除的项目? - JavaScript: Removing three items from an array at the same time. How to keep track of all the removed items? 如何在Promise函数JavaScript内的数组中推送项目 - How to push items in an array inside a promise function JavaScript 如何在Firebase中的所有子节点之间循环并在Javascript中读取其中的所有数据节点? - How would I cycle through all child nodes in Firebase and read all data nodes inside them in Javascript? 在Mongo中,我如何将集合的所有项目与更大的阵列匹配? - In Mongo, how would I match all items of collection against a larger array? 如何返回对象数组内的所有项目 - How to return all items inside array of object 如何使用php array_chunk和javascript过滤器将项目保存在一行中? - How do i keep items in one row using php array_chunk and javascript filter? 如何从函数内的数组调用项目 - How to call items from an array inside a function 数组不显示其中的所有项目 React JavaScript - Array doesnt display all of the items inside it React JavaScript 如何从 javascript 中的 map 中的异步 function 中获取产品价值 - How would I get the product value out of a async function inside a map in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM