wankdanker / node-object-mapper

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

Multi-level arrays do not respect parent array index #58

Open switzer opened 5 years ago

switzer commented 5 years ago

When trying to map multi-level arrays, as follows:

var from =
{ Items:
    [
        { SubItems:
            [
                { SubKey: 'item 1 id a' },
                { SubKey: 'item 1 id b' }
            ]
        },
        { SubItems:
            [
                { SubKey: 'item 2 id a' },
                { SubKey: 'item 2 id b' }
            ]
        }
    ]
}

I am trying to transform the names of the keys, keeping them in the same place in the array, like the following mapping example:

var map = {
    'Items[].SubItems[].SubKey': 'items[].subitems[].subkey',
}

The results I expect are:

var expected =
{ items:
    [
        { subitems:
            [
                { subkey: 'item 1 id a' },
                { subkey: 'item 1 id b' },
            ]
        },
        { subitems:
            [
                { subkey: 'item 2 id a' },
                { subkey: 'item 2 id b' },
            ]
        }
    ]
}

Unfortunately, the array items in the sub array all get grouped into the same parent item, as follows:

{ items:
   [ { subitems:
        [ { subkey: 'item 1 id a' },
          { subkey: 'item 1 id b' },
          { subkey: 'item 2 id a' },
          { subkey: 'item 2 id b' } ] } ] }