diff --git a/src/object-mapper.js b/src/object-mapper.js index 041eeed..63f90a0 100644 --- a/src/object-mapper.js +++ b/src/object-mapper.js @@ -85,7 +85,8 @@ function select_arr(src, key, keys) // Check to see if we are at a 'leaf' (no more keys to parse). If so, return the data. If not, recurse var d = (keys.length) ? select(src[i], keys.slice()) : src[i] // If the data is populated, add it to the array. Make sure to keep the same array index so that traversing multi-level arrays work - if (d !== null) + // If data is null, the subsequent steps will take the default value if there is one, and null if null is allowed. + if ( typeof d !== 'undefined') data[i] = d } diff --git a/test/test.js b/test/test.js index 38d22f9..551483c 100644 --- a/test/test.js +++ b/test/test.js @@ -2653,6 +2653,52 @@ test("issue #74: mapping empty array should result in empty array", t => { const result = om(src, map); + t.deepEqual(result, expect); + t.end(); +}); + +test('Ensure that null value check is iterated correctly in arrays', function (t) { + const src = { + "source": [ + { + "some": "value", + "empty": null + }, + { + "some": "value", + "empty": null + }, + { + "some": "value", + "empty": null + } + ] + }; + + const map = { + "source[].some": "destination[].some", + "source[].empty": "destination[].empty?" + }; + + const expect = { + "destination": [ + { + "some": "value", + "empty": null + }, + { + "some": "value", + "empty": null + }, + { + "some": "value", + "empty": null + } + ] + } + + const result = om(src, map); + t.deepEqual(result, expect); t.end(); }); \ No newline at end of file