Reputation: 3774
I have these items here that I want to push to an array in a set format. I have declared my array this way
finalCount = [];
Now I want this array to have items in this format: animal: Count For e.g: dog: 3 cat: 4 cow: 1
And each item in the array should be accessible by providing a index number as well. for e.g finalCount[0] should pick dog. I tried to push items this way
this.finalCount.push(this.animal, this.count);
but each item here doesn't go together. How do I get that.
Upvotes: 0
Views: 32
Reputation: 1725
To push items in your final count array, you can leverage synthax originally introduced with ES2015, computed properties
Now if you want to define the TypeScript type for objects in the finalCount array. You should create the following interface:
interface FinalCountItem {
[animal: string]: number;
}
finalCount: FinalCountItem[] = [];
this.finalCount.push({[this.animal]: this.count});
Upvotes: 1