Forum Discussion

acesario's avatar
acesario
Contributor II
5 years ago
Solved

How to identify matching values to create sequence numbers?

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”
]
}
]
}
]

  • @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]

15 Replies

  • viktor_n's avatar
    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))

    Output:

  • @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]

    • acesario's avatar
      acesario
      Contributor II

      @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?

  • acesario's avatar
    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”]

    • viktor_n's avatar
      viktor_n
      Contributor II

      @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())})

  • bojanvelevski's avatar
    bojanvelevski
    Valued Contributor

    If I understand correctly, this is the right output format?

    If that so, you can try the following expression:

    $Records.map(record => {“Award”: record.Award, “Sequence”: record.Award.map((x, index) => (record.Award.slice(0, index + 1).filter(y => y == x).length.toString()).length == 1 ? (“0” + record.Award.slice(0, index + 1).filter(y => y == x).length.toString()) : (record.Award.slice(0, index + 1).filter(y => y == x).length.toString()))})

    It’s a bit complex, but only because of it’s functionality to check the sequence number. If it’s one digit number, it will add “0” in front of it, if not, it will show the actual number.

    I surely hope that this will help,
    BR,
    Bojan

    • acesario's avatar
      acesario
      Contributor II

      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!

    • viktor_n's avatar
      viktor_n
      Contributor II

      My recommendation is to learn as much as you can of them, but I think most of the times are used Arrays, Strings, Objects, Dates.

  • acesario's avatar
    acesario
    Contributor II

    No, more like :

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

    “Award”: [“Award_A”,“Award_C”,“Award_A”,“Award_B”,“Award_C”],
    “Sequence”: [“01”,“01”,“02”,“01”,“02”]

    “Award”: [“Award_Z”,“Award_C”,“Award_A”],
    “Sequence”: [“01”,“01”,“02”]

    “Award”: [“Award_B”,“Award_C”,“Award_B”],
    “Sequence”: [“01”,“01”,“02”]

    “Award”: [“Award_A”,“Award_B”,],
    “Sequence”: [“01”,“01”]