unclechu / node-deep-extend

Recursive extend module
MIT License
202 stars 53 forks source link

Array elements gets duplicated in deep extend #11

Closed TejasCMehta closed 9 years ago

TejasCMehta commented 9 years ago
var book={};
var book.info = [ 
        {
            "title" : "Session 1",
           "description" : "XYZ"
        }, 
        {
            "title" : "Session 2",
          "description": "YYY"
        }
    ]

var dataToSave ={data:[]}

extend(dataToSave, book);

it shows: 

dataToSave.data = [ 
        {
            "title" : "Session 1",
           "description" : "XYZ"
        }, 
        {
            "title" : "Session 1",
           "description" : "XYZ"
        }
    ]

As you can see final data gives same elements in array, Please help me out!

Thanks,

unclechu commented 9 years ago

Which versions of deep-extend and node.js (or other js engine) do you use?

Here example:

#!/usr/bin/env node

// tag v0.2.10
// commit 86ca842...
var extend = require('./node-deep-extend');

var book = {};
book.info = [
        {
            "title": "Session 1",
            "description": "XYZ"
        },
        {
            "title": "Session 2",
            "description": "YYY"
        }
    ];

var dataToSave = {data:[]};

extend(dataToSave, book);

console.log(dataToSave);

And I have in stdout:

{ data: [],
  info: 
   [ { title: 'Session 1', description: 'XYZ' },
     { title: 'Session 2', description: 'YYY' } ] }

And here my node.js version:

$ node -v
v0.10.33
TejasCMehta commented 9 years ago

I am on 0.10.22 node js. and in my corrected example is:

var book={};
var book.info = [ 
        {
            "title" : "Session 1",
           "description" : "XYZ"
        }, 
        {
            "title" : "Session 2",
          "description": "YYY"
        }
    ]

var dataToSave ={info:[]}

extend(dataToSave, book);

it shows: 

dataToSave.info = [ 
        {
            "title" : "Session 1",
           "description" : "XYZ"
        }, 
        {
            "title" : "Session 1",
           "description" : "XYZ"
        }
    ]
unclechu commented 9 years ago

@TejasCMehta I can't understand how your code works. Baceuse

var book.info = [ 

is not valid javascript code. Check your code. If it really works like this it's node.js syntax parser bug. Please try my example and show here what you have in stdout. Did you even try compile your example as it is?