Copying of an array of objects to another Array without object reference in javascript(Deep copy)

后端 未结 7 510
说谎
说谎 2020-12-01 05:05

I have a scenario where i need to copy the array of Objects(Main array) to another Temp array which should not have object reference basically if i make any modification to

7条回答
  •  借酒劲吻你
    2020-12-01 05:35

    So you want a deep copy without object reference? Sure, use .slice().

    Example:

    var mainArr = [],
        tmpArr = []
    
    tmpArr = mainArr.slice(0) // Shallow copy, no reference used.
    

    PS: I don't think double-JSON parsing is performance wise.

提交回复
热议问题