cancel
Showing results for 
Search instead for 
Did you mean: 

To replace more than one character in string by looking up another field value

RaymanPrince
New Contributor II

I have a business requirement, where we have below JSON in that, have to update object’s field called TRANSLATION_ID by looking up another fields called VARIABLE_ID and VARIABLE_VALUE. Here if both VARIABLE_ID and VARIABLE_VALUE are not null then replace TRANSLATION_ID (say [A] to be replaceed with value in the variable value) with corresponding values present in VARIABLE_VALUE.

Note: Here currently we have max of 2 replacement values in VARIABLE_VALUE but we expect it can be more than 10.

Input json :
[
{
“PHRASE_SEQ”: 629,
“PHRASE_ID”: “[Drug Name]”,
“PLACEMENT”: “12”,
“TRANSLATION_ID”: “[Drug Name]”,
“VARIABLE_ID”: “Drug Name”,
“VARIABLE_VALUE”: “str456”
},
{
“PHRASE_SEQ”: 630,
“PHRASE_ID”: “Store at [A]°C to [B]°C”,
“PLACEMENT”: “13”,
“TRANSLATION_ID”: “Store at [A]°C to [B]°C”,
“VARIABLE_ID”: “A,B”,
“VARIABLE_VALUE”: “2,8”
}
]

Expected output would be:
[
{
“PHRASE_SEQ”: 629,
“PHRASE_ID”: “[Drug Name]”,
“PLACEMENT”: “12”,
“TRANSLATION_ID”: “str456”,
“VARIABLE_ID”: “Drug Name”,
“VARIABLE_VALUE”: “str456”
},
{
“PHRASE_SEQ”: 630,
“PHRASE_ID”: “Store at [A]°C to [B]°C”,
“PLACEMENT”: “13”,
“TRANSLATION_ID”: “Store at 2°C to 8°C”,
“VARIABLE_ID”: “A,B”,
“VARIABLE_VALUE”: “2,8”
}
]

5 REPLIES 5

Cele
New Contributor II

Hi @RaymanPrince

You can try to use the expression below in a mapper

$VARIABLE_ID != null && $VARIABLE_VALUE != null && $.extend({ 'TRANSLATION_ID': $VARIABLE_ID.split(',').reduce((acc, cur, index) => acc.replaceAll([cur], $VARIABLE_VALUE.split(',')[index]),$TRANSLATION_ID) }) || $

RaymanPrince
New Contributor II

Hi @Cele

Many Thanks
The solution works as expected.

parthiban_jks
New Contributor II

Hi Cele,

Thanks you so much for the quick reply. Could you please explain about the reduce part in the above expression? I would be really helpful for us

Thanks,
Parthiban S

Cele
New Contributor II

HI @parthiban.jks

I will try to explain to you the best I can do

$VARIABLE_ID.split(',').reduce((acc, cur, index) => acc.replaceAll([cur], $VARIABLE_VALUE.split(',')[index]),  $TRANSLATION_ID)

.reduce() is an array function which accepts 2 arguments, first is the callback function(arrow funciton) and the second is the initialValue, so to use the reduce you’ll need an array, so what I’m doing is taking the $VARIABLE_ID and spliting by comma, so this will return an array of strings divided at the supplied separator(comma “,”).

The reducer walks through that array element-by-element,and at each step, using the currentValue of the array, finds that currentValue(‘A’, ‘B’, …) in $TRANSLATION_ID and repalce it with value present in VARIABLE_VALUE consequently, what I mean with this is that I’m also splitting the $VARIABLE_VALUE by comma, this will also return an array, so I can access the value, which needs to be replace in $TRANSLATION_ID, with the index of the first array consequently

.reduce((accumulator, currentValue, currentIndex, arr) => , initialValue)

{
  "PHRASE_SEQ": 630,
  "PHRASE_ID": "Store at °C to [Y]°C",
  "PLACEMENT": "13",
  "TRANSLATION_ID": "Store at [A]°C to [B]°C",
  "VARIABLE_ID": "A,B",
  "VARIABLE_VALUE": "2,8"
}

$VARIABLE_ID.split(',')    -->   ['A', 'B'] - array
$VARIABLE_VALUE.split(',') -->   ['2', '8'] - array

$VARIABLE_ID.split(',').reduce((acc, cur, index) => acc.replaceAll([cur], $VARIABLE_VALUE.split(',')[index]),  $TRANSLATION_ID)
                                    
 ['A', 'B'].reduce((acc, cur, index) =>  acc.replaceAll([cur], ['2', '8'][index]) ,  "Store at [A]°C to [B]°C"  )

With this syntax ['2', '8'][index] when index is 0 we will get the value '2',
in the next iteration the index will be increment by 1 so we will get the value '8'

In the first step(first iteration) the currentValue(cur) is ‘A’ and the currentIndex (index) is 0, initialValue = $TRANSLATION_ID or “Store at [A]°C to [B]°C”, and accumulator = the result from previous calls or the initialValue(in the first iteration) also value that we end with. In the function we updating the accumulator with new value, so acc.replaceAll([cur], $VARIABLE_VALUE.split(',')[index]) in the first iteration accumulator(acc) = “Store at [A]°C to [B]°C”, and replaceAll method returns a new string
with all matches of a currentValue [‘A’], replaced by a value form $VARIABLE_VALUE ‘2’. Because the index is 0 in the first iteration this syntax $VARIABLE_VALUE.split(',')[index] would give the value ‘2’ from $VARIABLE_VALUE(array). In the second interation the index will be 1, currentValue ‘B’ and accumulator will be updated withvalue from the previous call, that is “Store at 2°C to [B]°C”, and so on until the last element of the array is reached

I hope this will helps you to understand better about reduce function, if not let me know I will try to explain better

BR
Kristijan