wankdanker / node-object-mapper

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

Submapping or mapping with in a map #94

Open tmorford opened 2 years ago

tmorford commented 2 years ago

I have an object:

const srcA = [{ "person": { "firstName": "John", "middleName": "Michael", "lastName": "Doe" }, "contact": { "phones": [{ "type": "cell", "number": "999-888-7777" }, { "type": "home", "number": "666-555-4444" }, { "type": "work", "number": "333-222-1111", "extension": "123" } ], "emailAddresses": [{ "type": "personal", "email": "johnnyd@gmailtest.com" }, { "type": "work", "email": "jdoe@gmailtest.com" } ], "addresses": [{ "type": "home", "lineOne": "987 Home St.", "lineTwo": "Apt G", "city": "Hometown", "state": "CO", "zip": "80027", "country": "USA" }, { "type": "work", "lineOne": "654 Work Rd.", "lineTwo": "Suite 205", "city": "Workcity", "state": "CO", "zip": "80026", "country": "USA" } ] } }, { "person": { "firstName": "Anthony", "middleName": "Scott", "lastName": "Doe", "birthDate": "1991/02/29" }, "contact": { "phones": [{ "type": "cell", "number": "666-222-4444" }], "emailAddresses": [{ "type": "personal", "email": "tonyd@gmailtest.com" }], "addresses": [{ "type": "home", "lineOne": "3456 Living St.", "city": "Hometown", "state": "CO", "zip": "80027", "country": "USA" }] } } ]

I am trying to map: const mapA = { "[].person.firstName":"[].firstName", "[].person.middleName":"[].middleName", "[].person.lastName":"[].lastName", "[].person.birthDate":"[].birthDate", "[].contact.phones[]":"[]communication[]", "[].contact.emailAddresses[]":"[]communication[]", "[].contact.addresses[]":"[]communication[]" }

In automapper c# version I could map and then have a submap of part of the object to map also. What I am trying to do is map "[].contact.phones[]":"[]communication[]", "[].contact.emailAddresses[]":"[]communication[]","[].contact.addresses[]":"[]communication[]" but your system just kind of makes the phone, email and address all one node, and removes parts of the data. I would like to do somerthing like []communication[] : [{[].contact.phones[],[].contact.emailAddresses[],[].contact.addresses[] }] to give this results: "communication": [{ "communicationMethod": "Address", "communicationType": "Home", "addressLineOne": "1234 Rocky Road", "addressLineTwo": "Apt 7", "addressCity": "Blanket", "addressState": "CO", "addressZip": "80029", "addressCountry": "USA" }, { "communicationMethod": "EmailAddress", "communicationType": "Personal", "emailAddress": "robbie@email.com" }, { "communicationMethod": "Phone", "communicationType": "Work", "phoneNumber": "789-123-4444", "phoneExtension": "978" }

not this: [[{ "type": "home", "lineOne": "987 Home St.", "lineTwo": "Apt G", "city": "Hometown", "state": "CO", "zip": "80027", "country": "USA" }, { "type": "work", "lineOne": "654 Work Rd.", "lineTwo": "Suite 205", "city": "Workcity", "state": "CO", "zip": "80026", "country": "USA" } ], [{ "type": "home", "lineOne": "3456 Living St.", "city": "Hometown", "state": "CO", "zip": "80027", "country": "USA" } ]] app.zip