javascript - 为什么这个数组不打印?

标签 javascript arrays function

我的代码:

arr = [1,2,3,4,5,6]

function printArray(arr){
  arrAtr = "";
  for(count = 0; count < arr; count++){
    arrAtr += (String(arr[count]) + " ")
  }
  document.write(arrAtr);
}

printArray(arr)

这不会打印数组。我期待:

1 2 3 4 5 6

最佳答案

您的代码中有错误。

你必须更换

printArray(myArr)

printArray(arr);

并替换

for(count = 0; count < arr; count++){

for(count = 0; count < arr.length; count++){

关于javascript - 为什么这个数组不打印?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37349252/

相关文章:

javascript - Protractor 通过名称标签获取元素

javascript - 正在阅读 attr 类,但我只需要其中之一

javascript - 如何获取数组元素编号?

javascript - 编辑 : Selecting Multiple Classes then toggle classes for each of them

C - 输入 2 个值,返回 2 个值到 MAIN。交换

c++ - QT连接没有匹配的函数可以调用

javascript - 无法在 Fullcalendar 中获取 "id"值

java - 如何在数组中查找大于、小于或等于某个值的数字?

c - 将 *.data 文件读入动态扩展的结构数组***

php - 运行代码时 MySQL 函数中的语法错误