1

When I stored response.getReturnValue() in two attributes in lightning if any change applied on one attribute other attribute data also changing

 component.set("v.pricebookProducts", response.getReturnValue()); //getting pricebook  products
 component.set("v.pricebookProductsCopy", response.getReturnValue());

if i done any modification on the first attribute then the second attribute also changing.pls help me how to overcome this problem

Thanks in advance

2

This is because in javascript, if you assign complex (non-primitive) data-types, the assignment is reference types. So, change in copy of data will change the original data also. You can do:

Option 1: Spread syntax

I guess you are returning list of objects (array), so you can do:

component.set("v.pricebookProductsCopy", [...response.getReturnValue()]);

Option 2: (old method)

component.set("v.pricebookProductsCopy", JSON.parse(JSON.stringify(response.getReturnValue())));
0

Try deep cloning :

JSON.parse(JSON.stringify(response.getReturnValue()));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.