简体   繁体   中英

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. 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

You need to use push for it, like this:

 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
        }
      }
    }
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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