Skip to Content

Changing the model values locally without effecting the original model in SAPUI5

I am saving data in a model. Data has been fetched from a service call. I am setting that data on different dropdowns in the view. I am changing that data locally on a few checks and conditions but my values in the original data are also getting changed. How to stop that. I need my original data not to be effected.

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

1 Answer

  • Posted on Feb 22, 2019 at 06:18 AM

    HI Swapnil Garg

    Your code will be helpful!!

    The issue might be because in javascript, the objects & arrays are pass by reference.

    So to rectify your issue, you need to to the below:

    //For Objects
    var mCopiedObject = jquery.extend(true, {}, originalObject);
    // For Arrays
    var mCopiedArray = jquery.extend(true, [], originalArray);
    // Now you can manipulate the copied object and arrays which will not affect the original object & array



    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.