简体   繁体   中英

Deep copy Node JS object without reference

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 __eagerlyLoadedAssoci ations: [] _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_Ed itable: (...) 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: (...)

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

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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM