dregenor / jsonMapper

simple json mapper
MIT License
31 stars 8 forks source link

Mapping while array exists in the converter #17

Closed maxrks closed 4 years ago

maxrks commented 4 years ago

Hi, thanks for your working, given codes:

const _jmap = require('json-mapper');
const _h = _jmap.helpers;

 const json = { a: 1, b: 2, c: 3 };
const mapping = {
            x: "c", y: "b", z: "a",
            arr: [
                { k: _h.def('m'), v: _h.def(2) },
                { k: _h.def('n'), v: _h.def(4) }
            ]
        };
const conv = _jmap.makeConverter(mapping);
const ret = conv(json);
console.log(ret);

{x:3,y:2,z:1,arr:{k:'n',v:4}}

I hope the arr in the mapping would keep as it was, but it would return just the last element, how to correct the codes?

maxrks commented 4 years ago

const mapping = { x: "c", y: "b", z: "a", arr:_h.def( [ { k: 'm', v: 2 }, { k:'n', v: 4 } ]) };