【发布时间】:2018-08-03 20:10:59
【问题描述】:
我需要为每个对象添加一个数组属性。我搜索了很多,got this,但这是 AngularJs。我在下面尝试过但不起作用。任何帮助表示赞赏
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)
【问题讨论】:
-
可以使用一个对象的数组和对象...
object = {people: [], total: {}} -
你的问题不清楚,首先你想在数组末尾添加元素还是开始?另外你没有解释你得到什么错误,你确定这是将对象放入数组而不是数组类型错误的问题吗?
-
我编辑的答案现在正确吗?
标签: angular typescript