ER.SHASHI TIWARI ER.SHASHI TIWARI -3 years ago 159
TypeScript Question

How to push object on specific length using spared operator in Angular 4

I have a array and i'm trying to push a object on specific length like on zero index position but it is pushing on last of the array length.

this.tradingPartner = new TradingPartnerModel();
this.tradingPartners = [...this.tradingPartners, this.tradingPartner];

Answer Source

If you want to add an Object at specific place on array you should user 'splice' method, for example:

this.tradingPartner.splice(2, 0, this.tradingPartner);

will insert new Object at index 2

EDIT

If you want to add new element at start of current array using spread you should only reverse order:

this.tradingPartners = [this.tradingPartner, ...this.tradingPartners];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download