0

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!

4
  • 3
    console.log(Object.keys(row)[0].trim() === 'User ID') <-- why do you have .trim() there? Leading or trailing whitespace would explain the problem. Commented Dec 1, 2020 at 17:38
  • 2
    ...and in fact there is an invisible whitespace character at the start of the key. Commented Dec 1, 2020 at 17:39
  • 1
    Yes, you have a white space before U. Commented Dec 1, 2020 at 17:40
  • Special character that looks empty on browser - #ef bb bf (utf-8 character) Commented Dec 1, 2020 at 17:43

3 Answers 3

2

You have an extra invisible whitespace character in keys of all of your objects (at least in those 2 you wrote). I started to copy-pasteing your 'User ID' strings in console, and found out that in your data both User ID keys have this problem.

pasted into browser console

These invisible characters are rendered as red dots when pasted into my browser's console. I also checked 2 other keys in your data, and they seem to be clear; however, better do a .trim() on object keys when you form your data from a csv file.

FYI, the character that breaks your code is \ufeff, "Zero-width no-break space".

Sign up to request clarification or add additional context in comments.

Comments

1

when i copied your code before "User ID" there was one char with question mark and when removed it then row["User ID"] worked just fine. so please try it !

in this codesandbox as you can see here it works just fine

Comments

1

There's special character in front of U in 'User Id'. Please try to run this answer. You will see it works.

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 (let i = 0; i < users.length; i++) {
  const row = users[i];
  let userId = row['User ID'];
  console.log(userId);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.