Get last element in an array

Since pop() can't be used in the Value of a table column in {{ … }} is there another easy method to access the last element in an array?

Because this currentRow.reports[currentRow.reports.length-1].result is also not working Value of a table column in {{ … }}

Thanks!

@gab Welcome to the forum!
Can you post an example of that array? And are you attempting to populate the a column with a value from an array in another column?

1 Like

Hi Scott
example of array where I need to access the last element:

currentRow.reports[
    { "status": "pending",
    "reviewer": "Alice",
    "date" : "30. Aug 2022" 
    },
    { "status": "confirmed",
    "reviewer": "Bob",
    "date" : "31. Aug 2022" 
    }

Many thanks!

you need access to date? just to be sure

To any of the fields, let's say "status".
My actual array is a bit different, names were changed to protect the innocent.

Got it

Can you post one complete row of the table from the output as I am having trouble formatting the table based on the data you sent... forget currentRow for now, I just need one complete row to build a table like yours

I hope I got it right, I tried to create a very simple example and not share my full data :slight_smile:

[
  {
    "id": 1,
    "name": "Hanson Deck",
    "email": "hanson@deck.com",
    "sales": 37,
    "reports": [
      {
        "status": "pending",
        "reviewer": "Alice",
        "date": "11. Aug 2022"
      },
      {
        "status": "confirmed",
        "reviewer": "Bob",
        "date": "12. Aug 2022"
      }
    ]
  },
  {
    "id": 2,
    "name": "Sue Shei",
    "email": "sueshei@example.com",
    "sales": 550,
    "reports": [
      {
        "status": "new",
        "reviewer": "Alice",
        "date": "13. Aug 2022"
      },
      {
        "status": "confirmed",
        "reviewer": "Bob",
        "date": "24. Aug 2022"
      }
    ]
  }
]

OK so let's say that for each row you want to populate another column with the last value of the reports array, as an example, where name is sue Shei you want a column that will have Status of confirmed...? Is that correct?

Without answering that question yet, so far I am going to assume you need all values... so you can add Custom Column for status, reviewer, and date and in each within the Value field:

{{currentRow.reports.map(d=>d.status)}}
{{currentRow.reports.map(d=>d.reviewer)}}
{{currentRow.reports.map(d=>d.date)}}

Yes, something like that.

PS: In the meantime I created a transformer and used JS there, so my problem is solved for now. But I do hope there is a simple solution.

Glad you resolved and using the transformer is the solution, so post it here so that other people can benefit.

I fixed this using a Transformer where JS is allowed.

I do wish there was an direct inline solution.

Can you post your actual solution as in the code.... (edit if need be if it shows info you don't want to share....

Sure, here it is:

  1. In the Code panel, create a new JS Transformer, e.g. getReportStatus
    It should contain something like…

    var selectedRow = {{reportsTable.selectedRow.data}};
    return selectedRow.reports.pop().status;
    
  2. In the table, set the Value to {{getReportStatus.value}}

  3. Celebrate!

However, I highly recommend a quick read of the Transformers docs, it's concise and more helpful Transformers

And if someone at Retool knows or implements a way to get the last element of an array inside a {{ }} code block, I hope they post it here and update the array docs.

Hey @gab, if I understood correctly you just need this in a new custom column (or overwrite the reports column)

{{_.last(currentRow.reports).status}}
3 Likes

I have a same question but after reading replay my problem is resolve.

3 Likes