通过阵列循环时,我希望将元素推向新数组。但是数组保持空
当我运行此循环时,它将带入一个包含八个数字的数组,它循环循环,然后将它们推到命名结果的全局数组中。由于某种原因,当我委托时。我希望它能从另一个数组返回八个数字。
const results = []
const validate = arr => {
for(let i = 0; i < arr.length; i++){
results.push(arr[i])
}
}
console.log(results)
When I run this loop it takes in an array which contains eight numbers, it loops through and simply pushes them to a global array which is named results. For some reason when I console.log this array outside of the loop it returns 0 []. I wish it to return the eight numbers from the other array.
const results = []
const validate = arr => {
for(let i = 0; i < arr.length; i++){
results.push(arr[i])
}
}
console.log(results)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您在这里所做的只是定义一个函数,该函数复制已传递到其中的数组的内容。您需要调用功能
验证
具有适当的值。What you have done here is simply define a function that copies the contents of the array that has been passed into it. You need to call the function
validate
with the appropriate value.那是因为您必须运行函数验证,现在您只是打印
result []
没有项目。但是,如果您想制作副本,则可以使用:
That is because you have to run the function validate, now you are just printing
results[]
which has no items.But if you want to make a copy, you can just use:
您错过了调用功能。
You missed calling the function.
如果您包含了数字的原始数组,则您的代码将起作用,并调用迭代的函数:
但是, map() 方法旨在做这种类型的事情:
Your code would work if you included the original array of numbers and you call the function that iterates over it:
But, the
Array.prototype.map()
method was designed to do just this type of thing: