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