Return first item from array for subitems of array

Options
Hello, Xano! I need help for noobs 🙂

I have a table of products and in API call each product contains a list of photos.
[
   {
      id: 2,
      name: name,
      photos: [
      {url},
      {url}
      …
]
I want to have only 1 photo (first element of photo array can have ID 0, or 1 if 0 was deleted) instead of list of photos [array] in response of this API call.
How to do this?
Thanks in advance!

Comments

  • Umair Kamil
    Umair Kamil Member
    Options
    Hi, so at the point where you are mapping the photo to your Add Record function, you would have to select a "Filter", specifically called "first"

    This filter gets the first entry of an array.
      - Please do confirm if this is correct

    [Screen Shot 2022-06-09 at 13.04.36 PM.png]
  • Chris Coleman
    Chris Coleman Administrator

    ADMIN

    Options
     Looks good, thank you Umair!
  • Anna Sho
    Anna Sho Member
    Options
    Thanx, I'll try!
  • Anna Sho
    Anna Sho Member
    Options
      sorry for asking again. It seems for me too complicated since Im total noob)

    I need that function for response of GET API call not POST, table itself shouldnt be changed, I need  to display list of products with 1 photo only in schema not array of photos (issue in my frontend Draftbit) and I can reuse that function for other tables too..

    if you have such awesome screenshot for my case that would be great!
  • Chris Coleman
    Chris Coleman Administrator

    ADMIN

    Options
     Hi, Anna. You should still be able to apply the same principle above on a GET request.
  • Anna Sho
    Anna Sho Member
    Options
     but instead ADD Record from Products there should be GET record right?
  • Chris Coleman
    Chris Coleman Administrator

    ADMIN

    Options
     I understand now, thanks for clarifying. For this, you could create a variable using dot notation (which would allow you to basically select the first image in the array).

    In this example, I have a table called 'deal', with an image object as a list. If I query the entire record, I get all three images I have stored in that one record. But, if I get the record, and then create a variable to extract only the first image, using dot notation, like this[image.png]"deal" - the table
    "images" - the object storing the images
    "0" - getting the first image in the list
    "url" - getting the URL of the image
    [image.png]
  • Anna Sho
    Anna Sho Member
    Options
     Awesome! I'll try!
  • David Juliano
    Options

    The problem in my case is that the initial GET request is too large, if I pull in all the list items my function is far too slow - anyone know of a way to just grab the first event initially? I guess a work-around could be to run a task every day that grabs the first item in this list and sets it to another field, but seems like a hefty solution to a simple problem