map-factory
map-factory copied to clipboard
Map to target array at specific index
Hi
I have a use case where I need to map source to a nested array at a specific index. I tried the following but it does not map to the target field lineItems[].products[0].name
. Also is there a better way to apply conditions with array?
const mapper = createMapper({alwaysSet: true});
const src = {
sources: [
{
name: 'item1',
product: {id: '1'},
upFrontFee: 123,
permFee: 123,
permPercentage: 123,
workerStatus: 'active',
publicHoliday: 'active',
illness: 'active',
optionType: null,
},
{
name: 'item2',
product: {id: '2'},
optionType: 'standard',
},
{
name: 'item3',
product: {id: '3'},
upFrontFee: 12,
permFee: 13,
permPercentage: 13,
workerStatus: 'active',
publicHoliday: 'active',
illness: 'active',
optionType: null,
},
{
name: 'item4',
product: {id: '4'},
optionType: 'standard',
},
],
};
mapper
.map(['sources[]', 'sources[].name'])
.to('lineItems[].products[0].name', (sources: any[], name: string[]) => {
const values = sources.filter(
(source: any) => name.includes(source.name) && source.optionType === null
);
return values.map((value: any) => value.name);
});
I expect the result to be the following
{
"lineItems":[
{
"products":[
{
"name":"item1"
}
]
},
{
"products":[
{
"name":"item3"
}
]
}
]
}
Actually, result:
{
"lineItems":[
{
"products":[]
}
]
}