摘要:无法使用括号表示法访问对象键值,因为现有键作为未定义返回。
我有一组用户(通过解析 CSV 文件及其列构建):
let users = [
{
'User ID': '5ef62675b78d747c79086175',
'Survey Completed Date': '11/12/19',
'Survey Type': 'Assessment'
},
{
'User ID': '5ef62675b78d827c79086186',
'Survey Completed Date': '27/12/19',
'Survey Type': 'Assessment'
},
...
];
对于每个用户,我需要访问他们的用户 ID 才能进行猫鼬查询。
console.log(users) // Logs the above object
for (let i = 0; i < users.length; i++) {
const row = users[i];
// Example with i = 0
console.log(row) // {
// 'User ID': '5ef62675b78d747c79086175',
// 'Survey Completed Date': '11/12/19'',
// 'Survey Type': 'Assessment'
// }
console.log('User ID' in row) // false (??) <<<<<<<<<
console.log(row['User ID']) // undefined (!?!) <<<<<<<<<
console.log(row['Survey Completed Date']) // 11/12/19 (This works however)
console.log(Object.keys(row)[0]) // User ID
console.log(Object.keys(row)[0].trim() === 'User ID') // true
console.log(row[Object.keys(row)[0]]) // 5ef62675b78d747c79086175 (want to
// avoid using this workaround since
// the properties will not always be
// in the same order)
let userId = row['User ID'] // (Still undefined)
let user = await User.findById(userId);
我有点卡住了。任何帮助或重定向将不胜感激!
慕桂英3389331
慕沐林林
守着星空守着你
相关分类