cancel
Showing results for 
Search instead for 
Did you mean: 

How to identify matching values to create sequence numbers?

acesario
Contributor II

Looking for a function to help add sequence numbers to a json array. End goal is to apply a sequence to matching values, starting with 01, subsequent values get a sequence of 02…03… 04 etc. I could do this in a mapper, or in apache velocity as part of a soap execute if possible to loop through, and apply therein.

What I have now:
[
{
“Records”: [
{
“Award”: [
“Award_A”,
“Award_C”,
“Award_A”,
“Award_B”,
“Award_C”
]
}
]
}
]

Desired output:
[
{
“Records”: [
{
“Award”: [
“Award_A”,
“Award_C”,
“Award_A”,
“Award_B”,
“Award_C”
],
“Sequence”: [
“01”,
“01”,
“02”,
“01”,
“02”
]
}
]
}
]

1 ACCEPTED SOLUTION

j_angelevski
Contributor III

@acesario , you can also just add [0] after @viktor_n’s expression.

$Records.map(record => record.Award.map((x, index) => record.Award.slice(0, index + 1).filter(y => y == x).length))[0]

And if you really need the “0” you can try and add .map(val => "0" + val)

So full expression is: $Records.map(record => record.Award.map((x, index) => record.Award.slice(0, index + 1).filter(y => y == x).length).map(val => "0" + val))[0]

View solution in original post

15 REPLIES 15

viktor_n
Contributor II

Hi @acesario,

Hope this will solve your problem.

Expression:
$Records.map(record => record.Award.map((x, index) => record.Award.slice(0, index + 1).filter(y => y == x).length))
Answer1

Output:
Output1

acesario
Contributor II

@viktor_n Thanks for the response, great use of map approach! One thing I notice is that your resulting Sequence has a separate array for the sequence values [[1,1,2,1,2]] rather than
“Sequence”: [“01”,“01”,“02”,“01”,“02”]

@acesario try this one.
$Records.map(record => {"Award": record.Award, "Sequence": record.Award.map((x, index) => record.Award.slice(0, index + 1).filter(y => y == x).length.toString())})

j_angelevski
Contributor III

@acesario , you can also just add [0] after @viktor_n’s expression.

$Records.map(record => record.Award.map((x, index) => record.Award.slice(0, index + 1).filter(y => y == x).length))[0]

And if you really need the “0” you can try and add .map(val => "0" + val)

So full expression is: $Records.map(record => record.Award.map((x, index) => record.Award.slice(0, index + 1).filter(y => y == x).length).map(val => "0" + val))[0]