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

前端 未结 4 1504
悲&欢浪女
悲&欢浪女 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:32

    You can use for-of loop

    let users = [
    {
        firstName: "John",
        lastName: "Doe",
        age: 34
    },
    {
        firstName: "Jack",
        lastName: "Jackson",
        age: 23
    },
    {
        firstName: "Ann",
        lastName: "Watson",
        age: 24
    }
    ];
    function getUsers(users) {
        for (var user of users) {
             console.log(user.firstName + " is " + user.age + " years old!");
        }
    }
    
    getUsers(users);
    

提交回复
热议问题