wankdanker / node-object-mapper

Copy properties from one object to another.
MIT License
277 stars 73 forks source link

Handling non-existent sub-array elements of source object #41

Closed nagarajanchinnasamy closed 5 years ago

nagarajanchinnasamy commented 7 years ago

For the following code:

var om = require('object-mapper');

var map = {
    "arr[].id": "arr[].id",
    "arr[].arr[].id": "arr[].arr[].id",
    "arr[].arr[].arr[].id": "arr[].arr[].arr[].id"
};

var src = {
    "arr": [
        {
            "id": 1
        }
    ]
};

var dest = om(src, map);
console.log(JSON.stringify(dest));

as sub-array elements don't exist in src, the expected output would be:

{"arr":[{"id":1}]}

However, the generated output is:

{"arr":[{"id":1,"arr":[{"arr":[{}]}]}]}

I modified the code to fix this issue. Modifications do not break any of the tests :)

Will send a pull request. Please review. Thanks 👍

switzer commented 5 years ago

I've verified this as an issue and added your test. I hope to fix shortly.

switzer commented 5 years ago

This is fixed in my branch