【问题标题】:How to add property to each object of an array in typescript.?如何在打字稿中为数组的每个对象添加属性。?
【发布时间】: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


【解决方案1】:

新答案

你完全改变了你的问题,你应该提出一个新问题而不是改变整个概念。无论如何,如果您必须向数据对象添加新属性,则很有可能您的应用设计错误。

要添加新属性,您不要使用.push(),因为这是数组方法,而是您想为所有对象添加新属性。 您可以通过使用循环来做到这一点,例如:

for (var i = 0; i < this.people.length; i++) {
    this.people[i].total = 2; // Add "total": 2 to all objects in array
}

array .map()

this.people.map((obj) => {
    obj.total = 2;
    // or via brackets
    // obj['total'] = 2;
    return obj;
})

另外,如果您需要合并对象或添加更多未知属性,您可以使用循环或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, 
    });
}

老答案

Ecma5 与 ES6 兼容,看起来你可能不知道自己想做什么。

您将代码放入ngOnInit,因此请务必在此事件后致电您的console.log。您的代码显示您在组件类之外调用了 console.log(people[1].total),因此它甚至无法访问此属性。

此外,您不应该在一个数组中混合不同类型的对象 - 这就是制作 typescript 的原因,以避免在数组和对象中包含不同的东西。 稍后在循环调用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() 返回新数组长度在这种情况下您的新元素索引是多少,因为push 在数组末尾添加了新元素。

【讨论】:

  • 希望你现在有我的问题。
  • 已编辑答案。你完全改变了你的问题不要这样做。在发布问题之前,请三思而后行。添加道具、新元素等也与打字稿无关。 Typescript 只是在您的代码中添加“类型”以使其更清晰,没有像 babel 中那样的额外 polyfill。 Angular 是帮助创建漂亮的 js 应用程序的框架,您的问题与 typescript 或 angular bassicaly 完全无关,它只是纯 js。最后一件事 - 下次让你的问题更清楚更详细。
  • 很棒的解决方案......谢谢......你拯救了我的一天。
【解决方案2】:

Array.push 将元素添加到数组的末尾,因此您必须通过people[people.length-1].total 访问它

【讨论】:

  • 添加一个 MDN 链接可能。
  • 它应该被添加为一个新的属性
猜你喜欢
  • 2019-06-15
  • 2016-08-30
  • 2016-11-27
  • 2020-12-16
  • 1970-01-01
  • 2015-03-01
  • 2021-10-24
  • 2021-12-17
  • 1970-01-01
相关资源
最近更新 更多