3

I am using typescript to code in Angular2. I have this object:

export class Car{
    name: String;
    door: {
        position: String;
        id: Number;
    };
}

I have initialized the object following this steps:

constructor() {
    this.door= new Door();
}
export class Door{
    position: String;
    ID: Number
}

and it perfectly works. My problem begins when I try to initialize an array of objects

export class Car{
    name: String;
    door: {
        position: String;
        id: Number;
    };
    color: {
       one: String;
       two: String;

    }[]
}

and I try to do the same Edited

constructor() {
    for (var i = 0; i < 10; i++) {
        this.color.push(new Color);
    }
        this.door= new Door();
    }


export class Color{
    one: String;
    two: String;
}

The error is the following:

ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'push' of undefined TypeError: Cannot read property 'push' of undefined

3
  • 1
    If you want to add the new Color object to the array, you can do: this.color.push(new Color());. Note: color may not be the ideal name for an array. :-) Commented Jan 17, 2018 at 15:51
  • My problem is that when I try to access like this.car.color[0].one = "white", it says that it is undefined. I guess that I need to initialize the object previously and that is what I am trying to do Commented Jan 17, 2018 at 15:54
  • 1
    @ConnorsFan Actually its not an array it's a tuple type { one: String; two: String; }[] is an array Commented Jan 17, 2018 at 15:55

1 Answer 1

2

The problem is that you declare the color property as tuple with a single item of type {one: string; two: string}

To initialize the tuple you can use

this.color= [new Color()];

Or if you want to declare an array of the type you can use:

color: {
    one: String;
    two: String;
}[]

and initialize it with an empty array:

this.color= [];
// Push 10 elements in the array, you can replace 10 with how many elements you need.
for(let i = 0; i< 10; i++) this.color.push(new Color()); 

More information about tuples here

Sign up to request clarification or add additional context in comments.

5 Comments

At the end it solved the problem just for the first position of the array/tuple but when I try to access to the 2nd one it crash
Well the tuple you defined only has one element in theory. If you want an array use the second suggestion in my answer, and initialize with an empty array, and then push as may values in it as you want : ex, adding two elements: this.color= []; this.color.push(new Color(), new Color())
ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'push' of undefined
I m gonna edit the question so maybe will be more clear
You haven't initialized the array this.color= []; before the for

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.