Extend Object by Dynamically Adding columns based on delimited string

I have the following Input:

[
{
“Customer Super Key”: “CUST-XXX”,
“Customer Security Segment”: “BU00172;BU00173;BU00174;BU00176;BU00175”
}
]

I want to transform it to:

[
{
“Customer Super Key”: “CUST-XXX”,
“Customer Security Segment 1”: “BU00172”,
“Customer Security Segment 2”: “BU00173”,
“Customer Security Segment 3”: “BU00174”,
“Customer Security Segment 4”: “BU00175”,
“Customer Security Segment 5”: “BU00176”
}
]

I need to dynamically create the column names based on how many ; separated values I have in the string “Customer Security Segment” as that number can change. I know I will probably need to use Split and maybe Reduce and Extend but I’m having a tough time figuring it out. Can someone help me with this? Thank you!

Hi,

Using this expression in a Mapper snap:

$['Customer Security Segment'].split(";").reduce((accum, curVal, index) => accum.extend({[$['Customer Super Key'] + (index + 1)]: curVal }), {})

You can create the object you are looking for from the input given.

Thank you CJ!, that did the trick but it I also had to add an $.extend at the root level to achieve what I was going for, so my finally expression ended up looking like this in the mapper Snap:

$.extend($[‘Customer Security Segment’].split(";").reduce((accum, curVal, index) => accum.extend({["Customer Security Segment " + (index + 1)]: curVal }), {}))