简体   繁体   English

如何在对象数组中分配对象?

[英]How to assign object in array of objects?

I have this: 我有这个:

values = [];

In ts i have multiple services that they have been called in different time. 在ts中,我有多种服务,它们在不同的时间被调用。 I trie to add that values of objects in array, but that values can be changed and i dont want to add always them in array, i want to assign if the objects values has been changed. 我试图在数组中添加对象的值,但是可以更改值,并且我不想总是在数组中添加它们,我想分配对象值是否已更改。

 this.values = this.values.map(a=>) Object.assign(this.values, [{ title: 'E-mail', indicator: this.emailData.length }]);
this.values = this.values.map(a=>) Object.assign(this.values, [{ title: 'Assgnment', indicator: this.assignments.length }]);

and so on. 等等。 But it always add one object and thats it. 但是它总是添加一个对象,仅此而已。 ANy suggestion? 有什么建议吗?

EDIT: 编辑:

if i add this: 如果我添加这个:

this.values.push(
  { title: 'E-mail', indicator: this.emailData.length }
)

If value has been changed i will have multiple objects with title:'E-mail', because it will always push in array new objects even if only indicator has been changed and i dont want that 如果值已更改,我将有多个标题为“ E-mail”的对象,因为即使仅更改了指示符,它也将始终推送新对象数组,而我不希望那样

You need to use push for it, like this: 您需要使用push ,如下所示:

 loadValues(title: string, indicator: string) {
    let existingData = this.values.filter(x => x.title == title && x.indicator == indicator)
    if (existingData == null) {
      this.values.push(
        { title: title, indicator: indicator }
      )
    } else {
      let data = this.values.filter(x => x.title == title);

      for(var i=0; i< this.values.length;i++){
        if(this.values[i].title == data.title) {
          this.values[i].indicator = indicator
        }
      }
    }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM