Get an error when I call object's properties in Array

前端 未结 4 1583
悲&欢浪女
悲&欢浪女 2021-01-28 15:10

In typescript code I have an array with objects in it. When I call \"getUsers(users)\" function, it returns the result as I need, but in console I get this error \"Uncaught Type

4条回答
  •  逝去的感伤
    2021-01-28 15:28

    Your for loop exceeds the available array items. The heighest ID is users.length - 1.

    So the easiest way is to remove the equal-sign.

    Also think of using the buildt in forEach or map function like this:

    function getUsers( users ) {
      users.forEach( u => {
        console.log( `${ u.firstName } is ${ u.age } years old!` );
      } );
    }
    

提交回复
热议问题