Update Array Recursively

Hi,

I am trying to update an array of CDTs recursively.

Base Array : 

{
{name:"Alex",rollNumber:null},
{name:"Stephen",rollNumber:3},
{name:"Dave",rollNumber:4},
{name:"Steave",rollNumber:5},
{name:"Rosey",rollNumber:7},
{name:"John",rollNumber:8},
{name:"Nile",rollNumber:10}
}

Now, If I insert number 4  where rollNumber is null , if that number exists in CDT array then all numbers greater than 4 should increase by1 until all duplicates are change to unique number, but all numbers less than 4 should remain unchanged.

This is how numbers should change :

4->5,

5->6

Since there are no duplicates after 6 then increment should stop here,

Updated Array: {
{name:"Stephen",rollNumber:3},
{name:"Alex",rollNumber:4},
{name:"Dave",rollNumber:5},
{name:"Steave",rollNumber:6},
{name:"Rosey",rollNumber:7},
{name:"John",rollNumber:8},
{name:"Nile",rollNumber:10}
},

Please help me with it

  Discussion posts and replies are publicly visible

Parents Reply Children
  • To answer your questions, please have a look on the inline comments :

    Where is that data coming from? [ Data is coming form a table in database ]

    Is this the whole list or just a part of it? [ it is the whole list ]

    What is the meaning of that "rollNumber"? [ '"rollNumber" is an integer value field] 

    Is it used in other table as a foreign key or something? Sorting? [ It is not used in other table as foreign key, but used to sort the whole table data]

    Then, how is that data added? By a user in an interface? [ data is added from a dropdown in an interface]

    Hope this answers your question.

  • 0
    Certified Lead Developer
    in reply to shamima0001

    OK. So to keep the data in a specific order, the actual values do not matter as long as they are ascending and unique.

    A code snippet similar to the below, should update the rollNumbers. There is no complex logic necessary.

    a!forEach(
      items: local!list,
      expression: a!update(
        fv!item,
        "rollNumber",
        fv!index
      )
    )