Is there a way to exclude a function before pushing an object into an array in Typescript? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Is there a way to exclude a function before pushing an object into an array in Typescript? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

When I push an object into an array, it pushes the object and it’s methods too like makeaction(), but I don’t wanna see them inside the array, Is there a way to exclude them

I got this on console

[ Pilot {
makeaction: [Function],
_name: 'Michael',
_lstname: 'Kiddies',
_age: 50,
_weight: 80,
_expyear: 8,
vec: 
 Car {
   _price: 3500000,
   _weight: 1300,
   _speed: 360,
   _power: 650,
   _brand: 'Aston Martin',
   _model: 'Vulcan',
   _torque: 1200,
   _person: [Circular] } },
Pilot {
makeaction: [Function],
_name: 'Enzo',
_lstname: 'LaFerrari',
_age: 23,
_weight: 90,
_expyear: 20,
vec: 
 Car {
   _price: 3500000,
   _weight: 1200,
   _speed: 320,
   _power: 750,
   _brand: 'Ferrari',
   _model: 'FXXK',
   _torque: 900,
   _person: [Circular] } },
Pilot {
makeaction: [Function],
_name: 'Austin',
_lstname: 'Texas',
_age: 30,
_weight: 56,
_expyear: 2,
vec: 
 Ship {
   _price: 5000000,
   _weight: 15000,
   _speed: 50,
   _power: 7000,
   _shipname: 'Evelin Mærsk',
   _beam: 20,
   _lenghtov: 55,
   _person: [Circular] } }]

Also I Want to delete that _person: [Circular] inside the array

These are some of my Classes

Pilot.ts

 export class Pilot extends Person implements PersonInterface {
    private vec: Vehicle;
    private _expyear: number;

    constructor(name?, lstname?, age?, weight?, expyears?, vec?: Vehicle) {
        super(name, lstname, age, weight);
        this.vec = vec;
        this._expyear = expyears;
        vec.person = this;
    }

    get expyear(): number {
        return this._expyear;
    }

    set expyear(value: number) {
        this._expyear = value;
    }

    public makeaction = () => {
        return this.vec.makeaction();
    }
}

Car.ts

export class Car extends Vehicle {
    private _brand: string;
    private _model: string;
    private _torque: number;

    constructor(brand?, model?, torque?, weight?, speed?, pow?, price?) {
        super(weight, speed, pow, price);
        this._brand = brand;
        this._model = model;
        this._torque = torque;
    }

    get brand(): string {
        return this._brand.trim();
    }

    set brand(value: string) {
        this._brand = value;
    }

    get model(): string {
        return this._model;
    }

    set model(value: string) {
        this._model = value;
    }

    get torque(): number {
        return this._torque;
    }

    set torque(value: number) {
        this._torque = value;
    }

    public makeaction() {
        if (this.person.constructor.name == `Customer`) {
            return `The ${this.person.constructor.name} ${this.person.name} ${this.person.lstname} has bought the ${this.constructor.name} ${this.brand + ` ` + this.model} for only ${prf.convertMillions(this.price)}`;
        }
        else {
            return `The ${this.person.constructor.name} ${this.person.name} ${this.person.lstname} is driving the ${this.constructor.name} ${this.brand + ` ` + this.model}`;
        }
    }
}

Answer

The problem with public makeaction = () => {, and the reason why it shows up in the console, is that it is a class property not a prototype method. It’s a property created in the constructor and assigned an arrow function. You’ll want to make it a normal method:

public makeaction() {
    …

That way, it’s implicitly non-enumerable (in ES6 classes at least), and won’t be considered by the console pretty printer because it is inherited.

We are here to answer your question about Is there a way to exclude a function before pushing an object into an array in Typescript? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji