0

I have some code I simplified for this question.

    this.getDataRuleList.splice(this.count, 1, dataRuleData);
    console.log(this.getDataRuleList);
    this.count += 1;

getDataRuleList is returning an array of objects from a service. It is also being bound to a PrimeNg TurboTable without a problem.

    // get method to get service collection
    get getDataRuleList(): IDataRule[] {
      return this._dataRuleListService.dataRuleList;
    }

When I edit a row, I am trying to update an object in my bound array (getDataRuleList) by replacing the whole object with a new object. I'm using the splice method for this. The problem is, every object I replace becomes identical (see image for a better understanding). The value i'm sticking in the spliced value's place is different every time (dataRuleData) but all my array elements become this value. I'm assuming it has something to do with references but how can I avoid this happening?

image of issue

2 Answers 2

2

you can clone a new object

const cloneData = Object.assign({},dataRuleData);
this.getDataRuleList.splice(this.count, 1,cloneData);

if you need to use deep clone you can check this example

const deepCloneData = JSON.parse(JSON.stringify(obj1));
Sign up to request clarification or add additional context in comments.

2 Comments

That worked perfectly! Very easy and clean solution. Thanks so much.
@Michael I suggest you use @baj9032 answer, it's simple to use :)
2

You can es6 splice operator for the clone

example

const cloneData = {...dataRuleData};

Comments

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.