How to access the final item in a nested array using JavaScript for AirTable Scripting?

I’m new to JavaScript and working on an AirTable project. I’m trying to get info from an API call and update my AirTable. The tricky part is getting the latest vote data which is the last item in the ‘votes’ array.

Here’s a simplified version of what I’m trying to do:

let apiToken = 'my_secret_token'
let table = base.getTable('Bills')
let { records } = await table.selectRecordsAsync()

for (let record of records) {
  let response = await remoteFetchAsync(`https://api.example.com/?key=${apiToken}&bill=${record.id}`)
  let data = await response.json()
  
  // This is where I'm stuck
  const newestVote = data.votes[data.votes.length - 1]
}

I’m getting an error saying I can’t read the ‘length’ of undefined. I guess I’m not accessing the ‘votes’ array correctly?

I’ve got two main questions:

  1. How can I correctly set the newestVote variable to get the last item in the votes array?
  2. Is there a better way to get the last item in an array than using length - 1?

Any help would be awesome! I’m used to R, so JavaScript is still a bit confusing for me.

hey mia92, i’ve dealt with this before. try using optional chaining and the at() method:

const newestVote = data?.votes?.at(-1);

this should grab the last vote safely. if data or votes is undefined, it’ll just return undefined instead of throwing an error.

hope this helps! let me know if u need anything else :slight_smile:

Hey there, Mia92! I’ve been in your shoes before, struggling with JavaScript arrays in Airtable. Here’s what I’ve learned:

The error you’re getting suggests that ‘data.votes’ might not exist or might not be an array. To fix this, you can use optional chaining and the nullish coalescing operator:

const newestVote = data?.votes?.[data.votes?.length - 1] ?? null;

This checks if ‘data’ and ‘votes’ exist before trying to access the last item. If they don’t, it’ll return null.

As for your second question, a more modern way to get the last item is using the ‘at()’ method:

const newestVote = data?.votes?.at(-1) ?? null;

The -1 index gives you the last item, and it’s cleaner than using length - 1.

Hope this helps! Let me know if you need any clarification.

I’ve encountered similar issues when working with nested data structures in Airtable. Your approach is on the right track, but there are a few safeguards we can implement to make it more robust.

First, it’s crucial to validate the data structure before accessing nested properties. You can use optional chaining (?.) to safely navigate the object:

const newestVote = data?.votes?.[data.votes?.length - 1];

This prevents errors if ‘data’ or ‘votes’ are undefined.

For a more concise solution, consider using the Array.at() method:

const newestVote = data?.votes?.at(-1);

This elegantly retrieves the last element without explicitly calculating the array length.

Remember to handle potential edge cases, such as empty arrays or unexpected data formats. Always test your script thoroughly with various input scenarios to ensure reliability in production.