NodeJS data.map()提供行但不访问值

6rqinv9w  于 2023-01-04  发布在  Node.js
关注(0)|答案(1)|浏览(168)

我不知道我在这里做错了什么,但是我可以迭代行,但是在Map时不能访问对象中的值()通过他们。有人能帮忙吗?我试图实现的事情是将创建的这个表嵌入到我的HTML模板中,以便nodemailer发送邮件。但不幸的是,我不能写表中的行,因为值没有定义,但是filtered.map(el,index)'el'记录了各行。

const result = results.rows
          console.log(result);
          let filtered = {};
            teams.map(team=>{
              filtered[team.value] = {
                  nvCount: 0,
                  nvTaken: 0,
                  vCount: 0,
                  vTaken: 0,
                  team:''
              };
            });

        
        result.map(user => {
          if (user.preference.includes("NON-VEG")) {
            filtered[user.team].nvCount += 1; 
            filtered[user.team].team = String(user.team)
            if (user.taken === true) {
              filtered[user.team].nvTaken += 1;
            }
          } else if (user.preference.includes('VEG')) {
              filtered[user.team].vCount += 1;
              if (user.taken === true) {
                filtered[user.team].vTaken += 1;
            }
          }
          else{
            console.log('Skipped');
          }
        });

    let tableData = (
    '<table>' +
      '<thead>' +        
          '<th>Team</th>' +
          '<th>Non-Veg</th>' +
          '<th>Veg</th>' +
          '<th>Total</th>' +      
      '</thead>'
    ); 
    
    {[filtered]?.map((el, index) => {
      console.log(el);//gives rows
      console.log(el.nvCount);//undefined
      tableData += (
        '<tr>' +
          '<td>' + el.team + '</td>' +
          '<td>' + el.nvCount + '</td>' +
          '<td>' + el.vCount + '</td>' +
          '<td>' + el.nvCount + el.vCount + '</td>' +
        '</tr>'
      );
   
    }) 
  }  
    
    tableData +=  '</table>';

console.log的结果(el):

{
  Greeters: { nvCount: 2, nvTaken: 1, vCount: 0, vTaken: 0, team: 'Greeters' },
  Cleaning: { nvCount: 1, nvTaken: 0, vCount: 0, vTaken: 0, team: 'Cleaning' },
  Media: { nvCount: 0, nvTaken: 0, vCount: 1, vTaken: 0, team: '' },
  Intercession: { nvCount: 0, nvTaken: 0, vCount: 0, vTaken: 0, team: '' },
  'Kids-Church': { nvCount: 0, nvTaken: 0, vCount: 0, vTaken: 0, team: '' },
  Kitchen: { nvCount: 0, nvTaken: 0, vCount: 0, vTaken: 0, team: '' }
}
3yhwsihp

3yhwsihp1#

因为filtered是一个对象,所以应该使用for ... in循环来迭代它的键:

let tableData =
  '<table>' +
  '<thead>' +
  '<th>Team</th>' +
  '<th>Non-Veg</th>' +
  '<th>Veg</th>' +
  '<th>Total</th>' +
  '</thead>';

{
  for (const key in filtered) {
    const el = filtered[key];
    tableData +=
      '<tr>' +
      '<td>' +
      el.team +
      '</td>' +
      '<td>' +
      el.nvCount +
      '</td>' +
      '<td>' +
      el.vCount +
      '</td>' +
      '<td>' +
      el.nvCount +
      el.vCount +
      '</td>' +
      '</tr>';
  });
}

tableData += '</table>';

相关问题