-2

I have 4 level object structure which i need to place on Cache. Once data is retrieved from cache, need to manipulate it before sending the response.

For this how i can do deep copy without reference.

i tried _.clone and Object.assign . none of them worked

dbCharges (24) [model, model, model, model, model, model, model, model, model, model, model, model, model, model, model, model, model, model, model, model, model, model, model, model]

each model has internal objects

model dataValues: {Service_Provider_Location_Charge_Id: 2084, Sequence: 2, Visible_On_Screen: true, Possible_Values: "Commercial;COM,Private;PRI,UK Military;MIL,Non UK Military;NMIL", Default_Value: null, …} isNewRecord: true eagerlyLoadedAssociations: [] _changed: {Service_Provider_Location_Charge_Id: true, Sequence: true, Visible_On_Screen: true, Possible_Values: true, Default_Value: true, …} _modelOptions: {timestamps: true, validate: {…}, freezeTableName: false, underscored: true, underscoredAll: true, …} _options: {isNewRecord: true, _schema: null, _schemaDelimiter: ""} _previousDataValues: {Service_Provider_Location_Charge_Id: undefined, Sequence: undefined, Visible_On_Screen: undefined, Possible_Values: undefined, Default_Value: undefined, …} Charge: Object ChargeType: model dataValues: {Charge_Type_Id: 2, Description: "Checkbox", Is_Active: true, Created_On: Fri Apr 01 2016 07:35:00 GMT+0100 (British Summer Time), Created_By: "SYSTEM", …} isNewRecord: true __eagerlyLoadedAssociations: [] _changed: {Charge_Type_Id: true, Description: true, Is_Active: true, Created_On: true, Created_By: true, …} _modelOptions: {timestamps: true, validate: {…}, freezeTableName: false, underscored: true, underscoredAll: true, …} _options: {isNewRecord: true, _schema: null, _schemaDelimiter: ""} _previousDataValues: {Charge_Type_Id: undefined, Description: undefined, Is_Active: undefined, Created_On: undefined, Created_By: undefined, …} After_Decimals: (...) Charge_Type_Id: (...) Created_By: (...) Created_On: (...) Datatype: (...) Description: (...) Is_Active: (...) Modified_By: (...) Modified_On: (...) Total_Length: (...) TypeDescription: (...) sequelize: (...) __proto: Model Charge_Id: 2 Charge_Type_Id: (...) Created_By: (...) Created_On: (...) Default_Value: (...) Default_Value_Rule_Id: (...) Default_Value_Rules: (...) Formula_Description: (...) Formula_Is_Active: (...) Formula_Name: (...) Formula_Notes: (...) Help_Text: (...) Invoice_Description: (...) Is_Active: (...) Is_Editable: (...) Is_Editable_Rules: (...) Is_Visible_On_Delivery_Ticket: (...) Local_Description: (...) Modified_By: (...) Modified_On: (...) Name_On_Delivery_Ticket: (...) Possible_Values: (...) Sequence: (...) Service_Provider_Location_Charge_Id: (...) Visible_On_Screen: (...) Visible_On_Screen_Rule_Id: (...) Visible_On_Screen_Rules: (...)

  • give the structure here – Renaldo Balaj Nov 28 '19 at 15:39
  • You'll have to post the code you attempted and explain further what you mean by "without reference". – Pointy Nov 28 '19 at 15:39
  • Have you looked at [What is the most efficient way to deep clone an object in javascript](https://stackoverflow.com/questions/122102/what-is-the-most-efficient-way-to-deep-clone-an-object-in-javascript) – Patrick Evans Nov 28 '19 at 15:42
  • 1
    Easiest way is to use `JSON.stringify` to convert your object into a JSON string, then use `JSON.parse` to get back your object without any reference – junwen-k Nov 28 '19 at 15:42
  • i have updated with obj structure.. we pick values from DB model and save in memory. We need to fetch obj from memory and modify it. For this purpose we need to copy this object without refernce so that obj cache should n't be modified – Phani Kumar Nov 28 '19 at 15:59

2 Answers2

0

_.clone only creates a shallow clone of the Object.
Try _.cloneDeep.

alexloehr
  • 1,773
  • 1
  • 20
  • 18
0

The methods you tried allow to make shallow copies, not deep copies. See this question for an explanation of the difference between deep copy and shallow copy.

To make a deep copy, you can use _.cloneDeep from Lodash.

const data = [{ 'a': 1 }, { 'b': 2 }];

const deepCopy = _.cloneDeep(data);

You can also make an deep copy without any library:

const deepCopy = JSON.parse(JSON.stringify(data));
Jérémie L
  • 770
  • 4
  • 14