【问题标题】:Can't get value of array无法获取数组的值
【发布时间】:2021-10-23 13:54:37
【问题描述】:

仍然无法理解我对这段代码的错误。 所有我想要的 - 通过 prompt 获取所有用户列表(姓名/姓氏)

function UserList() {
  let users = [];
  while (true) {
    let response = prompt('Please, enter your name surname?');
    if (response == null) {
      alert('cancel');
      break;
    }
    users.push(response.split(' '));
  }
  return users;
}

function User() {
  this.name = userList[0];
  this.surname = userList[1];
  this.regDate = new Date;
  for (i = 0; i < userList.length; ++i) {
    console.log('Name: ' + this.name + ' Surname: ' + this.surname + '. Date of registration : ' + this.regDate)
  }
}

let userList = new UserList();
let user = new User();

我遇到了一个误解,为什么尽管我输入了 users.push (response.split(' ')),但我无法获得 prompt 的第一个单词。 userList [0] - 显示数组的第一个 index 而不是第一个单词。

其次,我想在 console.log 中获取所有用户列表,但是根据数组的长度,我得到了相同的字符串

【问题讨论】:

    标签: javascript arrays split constructor


    【解决方案1】:

    userList[0] 函数 User 将返回一个数组:['name', 'surname']。 例如,要获取名字,您需要使用 this.name = userList[i][0]

    function UserList() {
        let users = [];
        while (true) {
            let response = prompt('Please, enter your name surname?');
            if (response == null) {
                alert('cancel');
                break;
            }
            users.push(response.split(' '));
        }
        return users;
    }
    
    function User() {
        for (var i = 0; i < userList.length; ++i) {
            this.name = userList[i][0];
            this.surname = userList[i][1];
            this.regDate = new Date;
            console.log('Name: ' + this.name + ' Surname: ' + this.surname + '. Date of registration : ' + this.regDate)
        }    
    }
    
    let userList = new UserList();
    let user = new User();
    

    【讨论】:

    • 是的,这是正确的答案!
    • 虽然它忽略了 User 应该代表 one 用户这一事实。在构造函数中迭代名称数组是没有意义的。 @axtck
    【解决方案2】:

    您正在将一个数组推送到另一个数组中,因此您的索引不正确(数组如下所示:[["firstname", "lastname"]])。推送时可以使用spread operator (...) 展开项目,也可以使用flat() 展平数组。

    同样在创建日期时,使用new Date()

     function UserList() {
       let users = [];
       while (true) {
         let response = prompt('Please, enter your name surname?');
         if (response == null) {
           alert('cancel');
           break;
         }
         users.push(...response.split(' ')); // flatten
    
       }
       return users;
     }
    
     function User() {
       this.name = userList[0];
       this.surname = userList[1];
       this.regDate = new Date(); // ()
    
       console.log('Name: ' + this.name + ' Surname: ' + 
         this.surname + '. Date of registration : ' + this.regDate)
     }
    
     let userList = new UserList();
     let user = new User();

    使用flat()

    return users.flat();
    

    编辑

    我实际上理解错了这个问题(以为你只想要 1 个用户),另一个答案应该是正确的并且更有意义。

    【讨论】:

      【解决方案3】:
      1. UserList 不应该是构造函数。它应该只是一个返回名称数组的函数。

      2. 你不应该遍历用户列表within User。然后,您应该遍历数组,在每次迭代时创建 一个新的User应该从构造函数生成。您可以从数组中传入每个名称并构建一个新对象。

      function getNames() {
        const users = [];
        while (true) {
          const response = prompt('Please, enter your first and last names');
          if (response == null) break;
          users.push(response.split(' '));
        }
        return users;
      }
      
      // Pass in a user name from the array as an argument
      // It's array so we can destructure the first and last name
      // immediately
      function User([first, last]) {
        this.first = first;
        this.last = last;
        this.regDate = new Date();
        this.message = `Name: ${this.first}. Surname: ${this.last}. Date of registration: ${this.regDate}.`;
      }
      
      // Iterate over the array generated by `getUsers`
      // and for each name create a new user.
      for (let name of getNames()) {
        console.log(new User(name));
      }

      其他文档

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-05-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多