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 , @j.angelevski
My initial testing worked with a limited record, however Iโ€™m noticing as I apply this to a more extended record structure, how the predicate of [0] impacts this. In this case, it always hits the first record, writing that result to each record. So if the first record has 2 awards, and the next has 5 it creates only two sequences on the second due to the [0]. Any ideas on how to define the predicate for the current record, rather than the first?

Just remove [0] at the end.

When I remove the [0] at the end it does produce all, but it changes the structure from to []. And as I was pushing this to jsonPath($, โ€œ$Records[*].Sequenceโ€), it no longer keeps sequence inline with the record.

acesario
Contributor II

This is great, I need to learn the map method much better I see. I found an initial tutoral here JavaScript Array map() Method, any other learning recommendations?

This worked perfectlyโ€ฆ not sure who to give the solution toโ€ฆ Viktor solved the basic approach problem, j dialed in. thanks to each of you!