英文:
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=>{
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>';
result of 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: '' }
}
答案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 =
'<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>';
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论