wankdanker / node-object-mapper

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

Mapping multiple fields to the same field in an object of an array #51

Open shwetajoshi601 opened 6 years ago

shwetajoshi601 commented 6 years ago

Hi, I am trying to map fields from different fields to fields of objects in an array into the destination object. Following is my example:

{
    "firstName": "S",
    "lastName": "J",
    "Alias": [{
        "firstName": "S111",
        "lastName": "J111"
    }, {
        "firstName": "S111222",
        "lastName": "J1222211"
    }],
    "address": [
        {
            "streetName": "",
            "city": "S2"
        },
        {
            "streetName": "karve",
            "city": "pune"
        }
    ]
}

The mapping I do is as follows:

{
   "firstName": "Name[].FirstName",
    "lastName": "Name[].LastName",
    "Alias[].firstName": "Name[].FirstName",
    "Alias[].lastName": "Name[].LastName",
    "address[].streetName": "Address[].StreetName",
    "address[].city": "Address[].City"
}

As you can see, I want certain top level fields as well as fields from an array to be mapped to a single array. So, the expected object is:

{
    "Name": [
        {
            "FirstName": "S",
            "LastName": "J"
        },
        {
            "FirstName": "S111",
            "LastName": "J111"
        },
        {
            "FirstName": "S111222",
            "LastName": "J1222211"
        }
    ],
    "Address": [
        {
            "StreetName": "",
            "City": "S2"
        },
        {
            "StreetName": "karve",
            "City": "pune"
        }
    ]
}

However, the result is:

{
    "Name": [
        {
            "FirstName": "S111",
            "LastName": "J111"
        },
        {
            "FirstName": "S111222",
            "LastName": "J1222211"
        }
    ],
    "Address": [
        {
            "StreetName": "",
            "City": "S2"
        },
        {
            "StreetName": "karve",
            "City": "pune"
        }
    ]
}

The first value gets overwritten in the result. Is there any option to help retain all the values? Can this issue be fixed asap @wankdanker ?

abhinav-juneja commented 3 years ago

@shwetajoshi601 Were you able to find another way to do it?