To generate an array using an interface, you can do the following:
interface Person {
name: string;
age: number;
}
const people: Person[] = [
{ name: "John", age: 30 },
{ name: "Jane", age: 25 },
{ name: "Bob", age: 40 }
];
This creates an array of Person
objects with different values for the name
and age
properties.
function generatePeople(numPeople: number): Person[] {
const people: Person[] = [];
for(let i = 0; i < numPeople; i++) {
people.push({
name: `Person ${i}`,
age: Math.floor(Math.random() * 50) + 20 // generate a random age between 20 and 69
});
}
return people;
}
const people = generatePeople(5); // generates an array of 5 Person objects with random names and ages
This function generates a specified number of Person
objects with random names and ages, and returns them as an array.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2021-04-15 11:00:00 +0000
Seen: 18 times
Last updated: Oct 02 '21
Can the names of rows be modified in MUI DataGrid?
How to modify the name of existing captions in MS Word?
How can you use linq to choose a specific column from a datatable?
What is the meaning of the role being returned as 'undefined' in DiscordJS?
What is the process of obtaining a column name by using a given row and its corresponding value?
What is the process for changing the name of a dropdown in a Grafana table query?