I want to output list of user from object. This object is array and when I don't use specific number of user I get invalid data. But when I put number for user in object I get information that I need. My question is, why my code does't work properly and how to output all user?
// Give Users needed types
type typeUsers = { name: string; age?: number };
// List of users
const Users: typeUsers[] = [
{
name: "John",
age: 21,
},
{
name: "Max",
},
];
function UserFunc(Users) {
// If user has age output this
if ("age" in Users) {
console.log(`Name: ${Users.name}, Age: ${Users.age}`);
} else {
console.log(`Name: ${Users.name}`);
}
}
UserFunc(Users);
But also its work when I change last string to UserFunc(Users[number])
CodePudding user response:
if ("age" in Users)
only works with objects, not with arrays. you can loop over the array like that:
// Give Users needed types
type typeUsers = { name: string; age?: number };
// List of users
const Users: typeUsers[] = [
{
name: "John",
age: 21,
},
{
name: "Max",
},
];
function UserFunc(Users: any) {
// If user has age output this
Users.forEach((User: any)=>{
if ("age" in User) {
console.log(`Name: ${User.name}, Age: ${User.age}`);
} else {
console.log(`Name: ${User.name}`);
}
})
}
UserFunc(Users);
CodePudding user response:
Users is an array. So in order to access an element in array you have to do Users[0].name or Users[1].name.
Or, if you want to output all the data, wrap your if else structure in an Array.forEach() like so:
// Give Users needed types
type typeUsers = { name: string; age?: number };
// List of users
const Users: typeUsers[] = [
{
name: "John",
age: 21
},
{
name: "Max"
}
];
function UserFunc(Users: typeUsers[]) {
// If user has age output this
Users.forEach((user) => {
if ("age" in user) {
console.log(`Name: ${user.name}, Age: ${user.age}`);
} else {
console.log(`Name: ${user.name}`);
}
});
}
UserFunc(Users);
CodePudding user response:
The problem is that you are checking for the User property age
directly from the array. As others have pointed out, you need to loop through the array to access each of the User object where the check for property age
will be valid.
// Give Users needed types
type typeUsers = { name: string; age?: number };
// List of users
const Users: typeUsers[] = [
{
name: "John",
age: 21,
},
{
name: "Max",
},
];
function UserFunc(Users) {
// If user has age output this
for (const user of Users) {
if ("age" in user) {
console.log(`Name: ${user.name}, Age: ${user.age}`);
} else {
console.log(`Name: ${user.name}`);
}
}
}
UserFunc(Users);
CodePudding user response:
First, it's common in javascript to write the variable names in camelCase
.
Second, Users
is an array
of object
, so you need to iterate over, to log each object
inside.
// Give Users needed types
type typeUsers = { name: string; age?: number };
// List of users
const users: typeUsers[] = [
{
name: "John",
age: 21,
},
{
name: "Max",
},
];
function userFunc(users: typeUsers[]) {
// loop throw users array with `forEach` method
users.forEach(user => {
// If user has age output this
if ("age" in user) {
console.log(`Name: ${user.name}, Age: ${user.age}`);
} else {
console.log(`Name: ${user.name}`);
}
})
}
userFunc(users);