簡體   English   中英

JavaScript object 上的括號表示法在現有鍵上返回未定義

[英]JavaScript bracket notation on object returning undefined on existing key

摘要:無法使用括號表示法訪問 object 鍵值,因為現有鍵返回為未定義。

我有一組用戶(通過解析 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 才能進行 mongoose 查詢。

  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);

我比較卡。 任何幫助或重定向將不勝感激!

摘要:無法使用括號表示法訪問 object 鍵值,因為現有鍵返回為未定義。

我有一組用戶(通過解析 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 才能進行 mongoose 查詢。

  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);

我比較卡。 任何幫助或重定向將不勝感激!

摘要:無法使用括號表示法訪問 object 鍵值,因為現有鍵返回為未定義。

我有一組用戶(通過解析 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 才能進行 mongoose 查詢。

  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);

我比較卡。 任何幫助或重定向將不勝感激!

摘要:無法使用括號表示法訪問 object 鍵值,因為現有鍵返回為未定義。

我有一組用戶(通過解析 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 才能進行 mongoose 查詢。

  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);

我比較卡。 任何幫助或重定向將不勝感激!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM