[英]How to add property to each object of an array in typescript.?
i need to add property to each object an array .我需要为每个对象添加属性数组。 i have searched a lot , got this , but this is AngularJs.我搜索了很多, 得到了这个,但这是 AngularJs。 I have tried below but not works.我在下面尝试过但没有用。 Any help 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)
NEW ANSWER新答案
You changed your question completely, you should probably make new question instead of changing whole concept.您完全改变了您的问题,您可能应该提出新问题而不是改变整个概念。 Anyway, if you have to add new properties to your data objects, there is high chance that your app is designed wrong.无论如何,如果您必须向数据对象添加新属性,则您的应用程序设计错误的可能性很高。
To add new property you don't use .push()
becasue this is array method, instead you wanted to add new property to all objects.要添加新属性,您不要使用.push()
因为这是数组方法,而是您想向所有对象添加新属性。 You can do this by using loop for example like this:您可以通过使用循环来做到这一点,例如:
for (var i = 0; i < this.people.length; i++) {
this.people[i].total = 2; // Add "total": 2 to all objects in array
}
this.people.map((obj) => {
obj.total = 2;
// or via brackets
// obj['total'] = 2;
return obj;
})
Also, if you need to merge objects or add more unknown properties you can use loop or Object.assign();
此外,如果您需要合并对象或添加更多未知属性,您可以使用 loop 或Object.assign();
for (var i = 0; i < this.people.length; i++) {
// merge objects into one with multiple props
this.people[i] = Object.assign(this.people[i], {
total: '2',
someProp: 'hello',
likePizza: true,
});
}
OLD ANSWER旧答案
Ecma5 is compatabile for ES6 and looks like you probably don't know what you want to do. Ecma5 与 ES6 兼容,看起来您可能不知道自己想要做什么。
You put your code in ngOnInit
so be sure you call your console.log
after this event.您将代码放在ngOnInit
因此请确保在此事件后调用您的console.log
。 Your code says you called console.log(people[1].total)
outside of your component class so it can't even access this property.您的代码说您在组件类之外调用了console.log(people[1].total)
,因此它甚至无法访问此属性。
Also you should not mix different types of objects in one array - this is why typescript was made, to avoid having different things in arrays and objects.此外,您不应该在一个数组中混合不同类型的对象 - 这就是制作 typescript 的原因,以避免在数组和对象中包含不同的内容。 Later in loop calling element[i].product
can casue error becasue your new object has no such property.稍后在循环中调用element[i].product
可能会导致错误,因为您的新对象没有这样的属性。
export class ThreeComponent implements OnInit {
people = [];
// people: Array<YourObjects>; // would be better
ngOnInit() {
this.people = [
{'name': 'person 1', 'product': 'Medicine 1'},
{'name': 'person 2', 'product': 'Medicine 2'},
{'name': 'person 3', 'product': 'Medicine 3'},
];
let newLength = this.people.push({'total' : '2'}); // returns new array length
console.log(this.people[newLength ].total); // it works in this case
}
}
.push()
returns new array length what is your new element index in this case becasue push
adds new elements at the end of array. .push()
返回新的数组长度,在这种情况下,您的新元素索引是多少,因为push
在数组末尾添加新元素。
Array.push
将元素添加到数组的末尾,因此您必须通过people[people.length-1].total
访问它
You have already have 3 element in people array. 人们数组中已经有3个元素。
I think you should use console.log(this.people[3].total);
我认为您应该使用console.log(this.people[3].total);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.