4

I have an array called "cart" of objects which looks like this,

cart:[{name:any, rate:number, qty:number, discount:number, subtotal:number}]

and I don't want to have any data in it at the beginning, but when I write

cart:[{name:any, rate:number, qty:number, discount:number, subtotal:number}];

and try to find the length by using

Object.keys(this.cart).length

it shows an error saying

`TypeError: Cannot convert undefined or null to object`

Also, how to clear all elements in an Array.

0

2 Answers 2

11

Based on my understanding, the following should work for you,

interface ISomething {
    name: any;
    rate: number;
    qty: number;
    discount: number;
    subtotal: number
}
let cart: Array<ISomething> = [];
Sign up to request clarification or add additional context in comments.

Comments

0

You need to first initialize the array:

cart = [];

Also, do this to clear all elements in the array.

Comments

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.