data.map()返回行但无法访问值。

huangapple go评论70阅读模式
英文:

data.map() giving rows but not accessing values

问题

我不知道我在这里做错了什么,但我能够遍历行,但在通过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: '' }
}
英文:

I don't know what I am i doing wrong here, but I'm able to iterate through the rows but cannot access the values in the objects when I map() through them. Can someone help out?
The thing i am trying to achieve is to embed this table which is created, into my HTML template for nodemailer to send a mail. But unfortunately, I'm not able to write the rows for the table since the values are undefined. But the filtered.map(el,index) 'el' logs the individual rows.

const result = results.rows
console.log(result);
let filtered = {};
teams.map(team=&gt;{
filtered[team.value] = {
nvCount: 0,
nvTaken: 0,
vCount: 0,
vTaken: 0,
team:&#39;&#39;
};
});
result.map(user =&gt; {
if (user.preference.includes(&quot;NON-VEG&quot;)) {
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(&#39;VEG&#39;)) {
filtered[user.team].vCount += 1;
if (user.taken === true) {
filtered[user.team].vTaken += 1;
}
}
else{
console.log(&#39;Skipped&#39;);
}
});
let tableData = (
&#39;&lt;table&gt;&#39; +
&#39;&lt;thead&gt;&#39; +        
&#39;&lt;th&gt;Team&lt;/th&gt;&#39; +
&#39;&lt;th&gt;Non-Veg&lt;/th&gt;&#39; +
&#39;&lt;th&gt;Veg&lt;/th&gt;&#39; +
&#39;&lt;th&gt;Total&lt;/th&gt;&#39; +      
&#39;&lt;/thead&gt;&#39;
); 
{[filtered]?.map((el, index) =&gt; {
console.log(el);//gives rows
console.log(el.nvCount);//undefined
tableData += (
&#39;&lt;tr&gt;&#39; +
&#39;&lt;td&gt;&#39; + el.team + &#39;&lt;/td&gt;&#39; +
&#39;&lt;td&gt;&#39; + el.nvCount + &#39;&lt;/td&gt;&#39; +
&#39;&lt;td&gt;&#39; + el.vCount + &#39;&lt;/td&gt;&#39; +
&#39;&lt;td&gt;&#39; + el.nvCount + el.vCount + &#39;&lt;/td&gt;&#39; +
&#39;&lt;/tr&gt;&#39;
);
}) 
}  
tableData +=  &#39;&lt;/table&gt;&#39;;

result of console.log(el):

{
Greeters: { nvCount: 2, nvTaken: 1, vCount: 0, vTaken: 0, team: &#39;Greeters&#39; },
Cleaning: { nvCount: 1, nvTaken: 0, vCount: 0, vTaken: 0, team: &#39;Cleaning&#39; },
Media: { nvCount: 0, nvTaken: 0, vCount: 1, vTaken: 0, team: &#39;&#39; },
Intercession: { nvCount: 0, nvTaken: 0, vCount: 0, vTaken: 0, team: &#39;&#39; },
&#39;Kids-Church&#39;: { nvCount: 0, nvTaken: 0, vCount: 0, vTaken: 0, team: &#39;&#39; },
Kitchen: { nvCount: 0, nvTaken: 0, vCount: 0, vTaken: 0, team: &#39;&#39; }
}

答案1

得分: 1

由于 filtered 是一个对象,您应该使用 for ... in 循环来迭代它的键:

let tableData =
  '<table>' +
  '<thead>' +
  '<th>队伍</th>' +
  '<th>非素食</th>' +
  '<th>素食</th>' +
  '<th>总计</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>';

这是对您提供的代码的翻译部分。

英文:

Since filtered is an object you should use a for ... in loop to iterate over its keys:

let tableData =
&#39;&lt;table&gt;&#39; +
&#39;&lt;thead&gt;&#39; +
&#39;&lt;th&gt;Team&lt;/th&gt;&#39; +
&#39;&lt;th&gt;Non-Veg&lt;/th&gt;&#39; +
&#39;&lt;th&gt;Veg&lt;/th&gt;&#39; +
&#39;&lt;th&gt;Total&lt;/th&gt;&#39; +
&#39;&lt;/thead&gt;&#39;;
{
for (const key in filtered) {
const el = filtered[key];
tableData +=
&#39;&lt;tr&gt;&#39; +
&#39;&lt;td&gt;&#39; +
el.team +
&#39;&lt;/td&gt;&#39; +
&#39;&lt;td&gt;&#39; +
el.nvCount +
&#39;&lt;/td&gt;&#39; +
&#39;&lt;td&gt;&#39; +
el.vCount +
&#39;&lt;/td&gt;&#39; +
&#39;&lt;td&gt;&#39; +
el.nvCount +
el.vCount +
&#39;&lt;/td&gt;&#39; +
&#39;&lt;/tr&gt;&#39;;
});
}
tableData += &#39;&lt;/table&gt;&#39;;

huangapple
  • 本文由 发表于 2023年1月4日 15:21:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75002100.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定