AutoMapper
AutoMapper copied to clipboard
How to map array of strongly typed objects in a one liner?
Hi,
Great library, I love it!
I am stuck at mapping an array of objects in a one liner. I want to achive something like this (pseudo code):
let destinationArray: DestinationType[]
= automapper.map(SourceType.name, DestinationType.name, sourceArray)
where sourceArray: SourceType[]
How can I map it in a one liner?
So far, my workaround is looping through the sourceArray and mapping each element individually:
const destinationArray: DestinationType[] = new Array(sourceArray.length);
for (let i = 0; i < sourceArray.length; i++) {
destinationArray[i] = automapper.map(SourceType.name, DestinationType.name, sourceArray[i]);
}
Have you tried?
let destinationArray: DestinationType[] = sourceArray.map(s => automapper.map(SourceType.name, DestinationType.name, s));
This library is supposed to be a direct port of the C# AutoMapper which handles collections automatically. If this library doesn't then that's a glaring omission.