Visual Builder

Get Involved. Join the Conversation.

Topic

    Stephen Bryant
    getting max value in array in javascript not workingAnswered
    Topic posted July 25, 2019 by Stephen BryantBronze Trophy: 5,000+ Points, last edited July 25, 2019, tagged Action Flows, Business Objects, Custom Code, REST, UI, Web 
    21 Views, 3 Comments
    Title:
    getting max value in array in javascript not working
    Summary:
    frustrating issue
    Content:

    Hello,

    First a little background.

    I have an editable array with an "add row" button.  The button adds a single row that is in a business object specifically for that purpose.  As users enter data in the grid, they can add a row for additional data entry.  The entries require unique ids for the array to operate properly.  Each time a new row is added, the id will always be the number 1 since that is in the business object.  I need to iterate over the array to get the max id and then add 1 to it and update the additional row.  So far so good.

    I have a simple function to read the array and generate a max id.  However, the function will not the return the max id.  I know I am reading the entire array as I can see the payload in dev tools and also return individual ids but when I had the max function, it does not work.

    This is simple so I am dumbfounded it does not work.

    Here is my code:

    PageModule.prototype.getID = function(array) {

        var idArray = [];

        for (var i = 0; i <= array.length - 1; i++) {
          
        idArray.push ({
           
           newId: array[i].id
           
           });    

        }
        
        return Math.max(idArray);     

    -------  I can return individual values here but it will not work with Math.max function and the array as a parameter.  If I enter numbers as                                                   parameters here, it will return the maximum.  Just not with the idArray as the parameter.        

      };

    Thanks,

    Steve

     

     

     

     

    Version:
    19.1.3

    Best Comment

    Shay Shmeltzer

    Wouldn't the max value be equal to the array.lenght ?

    Can you just return that as the new id?

    Comment

     

    • Shay Shmeltzer

      Wouldn't the max value be equal to the array.lenght ?

      Can you just return that as the new id?

    • Stephen Bryant

      Shay,

      Well sure it would but this situation it a tad different.  This is a customized edit screen that loads the original transaction with headers and lines.

      The lines have ids from the transaction business object so those ids come in.  However, when I add the row, the id is added as the number 1 as it would be for all rows added.  So the array of ids could look like this:

      [42, 43, 1]

      I wanted to read the max and add 1 and then assign that value to the new row and do that everytime a new row is added.  

      Since the ids are not sequential in this case, this is the way I believe I need to proceed.

      Do you have any suggestions?  The code is simple so shouldn't that work?  

      It is strange.

      Thanks,

      Steve

    • Stephen Bryant

      Shay,

      Actually, you're suggestion worked since the new row is not in the array!!!!!

      Thanks,

      Steve