【问题标题】:Print array which contains arrays打印包含数组的数组
【发布时间】:2017-01-11 21:54:12
【问题描述】:
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, arrSch2) {
  arrSch1.forEach(function callBack(nod1) {
    console.log(nod1);
  });
  arrSch2.forEach(function callBack(nod1) {
     console.log(nod1);
  });
});

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

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

【问题讨论】:

  • 您不需要arrSch2.forEach(),您已经在使用peprson.marks.forEach()arrSch1.forEach() 循环枚举二维数组。

标签: javascript arrays printing foreach


【解决方案1】:

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

这是一个不同的视角:

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

【讨论】:

    【解决方案2】:

    数组的第二个元素是 [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】:

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

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

      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);
        });
      });

      【讨论】:

        【解决方案4】:

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

        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 记住 peprson.marks 包含每个索引上的数组。祝你好运!!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-03-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-04-08
          • 2022-01-16
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多