Lodash .clone and .cloneDeep behaviors

前端 未结 1 1479
南方客
南方客 2020-12-08 09:27

I try to clone an array of objects with nested objects.

Something like:

var data = [
    { id: 1, values: { a: \'a\', b: \'b\' } },
    { id: 2, valu         


        
相关标签:
1条回答
  • 2020-12-08 10:02

    Thanks to Gruff Bunny and Louis' comments, I found the source of the issue.

    As I use Backbone.js too, I loaded a special build of Lodash compatible with Backbone and Underscore that disables some features. In this example:

    var clone = _.clone(data, true);
    
    data[1].values.d = 'x';
    
    • with the Normal build: _.isEqual(data, clone) === false
    • with the Underscore build: _.isEqual(data, clone) === true

    I just replaced the Underscore build with the Normal build in my Backbone application and the application is still working. So I can now use the Lodash .clone with the expected behaviour.

    Edit 2018: the Underscore build doesn't seem to exist anymore. If you are reading this in 2018, you could be interested by this documentation (Backbone and Lodash).

    0 讨论(0)
提交回复
热议问题