Summary: Unable to access object key value using bracket notation as the existing key is returned as undefined.
I have an array of users (constructed by parsing a CSV files and its columns):
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'
},
...
];
For every user, I need to access their User ID in order to make a mongoose query.
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);
I'm rather stuck. Any help or redirection would be greatly appreciated!

console.log(Object.keys(row)[0].trim() === 'User ID')<-- why do you have.trim()there? Leading or trailing whitespace would explain the problem.