I am facing a challenge involving transforming an array of type A into an array of type B and then passing it to a class called Person as input data. I am currently stuck and uncertain about how to accomplish this task.
Here is the definition of Type A and an array of this type:
type A = Array<[string, number, string]>;
const a: A = [
['Name1', 15, 'City1'],
['Name2', 44, 'City2'],
['Name3', 23, 'City3'],
['Name4', 73, 'City4'],
['Name5', 12, 'City5'],
['Name6', 37, 'City6']];
Type B:
type B = {
[id: string]: Person}
Definition of Class Person:
class Person {
_id: string; // must be unique
age: number;
name: string;
city: string;
constructor(data) {
if (data == null) {
console.log("No data presented")
} else {
this._id = data._id
this.age = data.age
this.name = data.name
this.city = data.city
}
}
tellUsAboutYourself() {
console.log(
`Person with unique id = ${this._id} says:\n
Hello! My name is ${this.name}. I was born in ${this.city}, ${this.age} years ago.`
);
}}
This is my attempt at solving the problem:
export const b: B[] = a.map(([name, age, city], index) => ({
[index]: new Person({ _id: index, name, age, city })}))
However, I am unable to call methods of the class using the following code:
for (let person of b) {
console.log(person.tellUsAboutYourself());
}