AutoMapper icon indicating copy to clipboard operation
AutoMapper copied to clipboard

How to map array of strongly typed objects in a one liner?

Open philippfx opened this issue 6 years ago • 2 comments

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]);
}

philippfx avatar Nov 05 '18 18:11 philippfx

Have you tried? let destinationArray: DestinationType[] = sourceArray.map(s => automapper.map(SourceType.name, DestinationType.name, s));

GravlLift avatar Dec 19 '18 14:12 GravlLift

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.

Neutrino-Sunset avatar May 22 '19 10:05 Neutrino-Sunset