打印包含数组的数组

时间:2017-01-11 21:54:12

标签: javascript arrays printing foreach

download.file(URL="", destfile="",mode='wb')

我收到错误:arrSch2.forEach不是函数

我实际上并不理解为什么arrSch2.forEach不起作用。不是标记数组的第二个值,值为[41,63,5];?

4 个答案:

答案 0 :(得分:0)

传递给forEach的第二个参数是index。所以,arrSch2实际上只是一个数字。数字上不存在forEach方法。

这是一个不同的观点:

peprson.marks.forEach(function callBack(arrSch1, index) {
    ...
}

答案 1 :(得分:0)

Array.prototype.forEach回调函数有三个参数:

  1. currentValue - 数组中正在处理的当前元素。 索引
  2. index - 数组中正在处理的当前元素的索引。
  3. array - 正在应用forEach()的数组。
  4. arrSch2是一个不是数组的数字。

    将您的代码更改为此 - 我删除了第二个forEach调用:

    
    
    function PersonAMK(vn, ln) {
    this.nachname = ln;
    this.vn = vn;
    this.marks = [];
    
    this.marks.push([4, 67, 5]);
    this.marks.push([41, 63, 5]);
    this.marks.push([4, 67, 55]);
    } 
    
    var peprson = new PersonAMK('Unknwon', 'Unknown');
    
    
    peprson.marks.forEach(function callBack(arrSch1) {
      arrSch1.forEach(function callBack(nod1) {
        console.log(nod1);
      });
    });
    
    
    

答案 2 :(得分:0)

数组的第二个元素是[41,63,5],但这不是你的参数中传递的内容。如果您只想迭代数组,那么类似下面的内容将起作用:

    function PersonAMK(vn, ln) {
    this.nachname = ln;
    this.vn = vn;
    this.marks = [];

    this.marks.push([4, 67, 5]);
    this.marks.push([41, 63, 5]);
    this.marks.push([4, 67, 55]);
    } 

    var peprson = new PersonAMK('Unknwon', 'Unknown');


    peprson.marks.forEach(function (arr) {
      arr.forEach(function (nod1) {
        console.log(nod1);
      });
    });

答案 3 :(得分:0)

为什么不使用简单的循环。

select case 
          when grouping(country) = 1 then 'Total' 
          else country 
       end as country, 
       count(*) 
from customer
group by rollup (country )

试试这个。使用function PersonAMK(vn, ln) { this.nachname = ln; this.vn = vn; this.marks = []; this.marks.push([4, 67, 5]); this.marks.push([41, 63, 5]); this.marks.push([4, 67, 55]); } var peprson = new PersonAMK('Unknwon', 'Unknown'); for (i = 0; i < peprson.marks.length; i++) { for (j = 0; j < peprson.marks[i].length; j++){ console.log(peprson.marks[i][j]); } }方法。为此提供以下输出:JSON.stringify()。您需要迭代{"nachname":"Unknown","vn":"Unknwon","marks":[[4,67,5],[41,63,5],[4,67,55]]}记住peprson.marks包含每个索引上的数组。祝你好运!!