0

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

1 Answer 1

1

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
        }
      }
    }
  }
Sign up to request clarification or add additional context in comments.

3 Comments

yes, and 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 indiciator has been changed and i dont want that
Modified the answer, call this function, this will check if the same value exists
I dont think that u understand me... i want to change value in array for that same title if indicator has been changed... so if i have title:'Email' and it have indicator :1, next time if indicator is 2 for title:'Email' i want to changed that value in array and not to insert new one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.