In my search for adding a property to each object in an array, I came across a solution in AngularJs on Stack Overflow. However, I attempted the following approach which did not yield the desired outcome. Any assistance would be greatly appreciated.
export class ThreeComponent implements OnInit {
people = [];
ngOnInit() {
this.people = [
{'name': 'person 1', 'product': 'Medicine 1'},
{'name': 'person 2', 'product': 'Medicine 2'},
{'name': 'person 3', 'product': 'Medicine 3'},
]
this.people.push({'total' : '2'})
console.log(this.people)
}
}
results look like this:
(4) [{…}, {…}, {…}, {…}]
0:{name: "person 1", product: "Medicine 1"}
1:{name: "person 2", product: "Medicine 2"}
2:{name: "person 3", product: "Medicine 3"}
3:{total: "2"}
length:4
__proto__:Array(0)
expected result should be:
(3) [{…}, {…}, {…}]
0:{name: "person 1", product: "Medicine 1", "total": "2"}
1:{name: "person 2", product: "Medicine 2", "total": "2"}
2:{name: "person 3", product: "Medicine 3", "total": "2"}
length:3
__proto__:Array(0)