Releases And Snippets

Lambdas (JavaScript)

Summary

Hey there! Michael from Xano here with an exciting update. You can now use lambdas (small snippets of JavaScript code) in your Xano function stack to supercharge your business logic. While you can still do everything the no-code way, lambdas offer an additional avenue or path for those who know a bit of JavaScript.

Let's dive into a quick example to see how lambdas can enhance your Xano experience.

Example: Combining First and Last Name

Imagine you have a list of player objects with separate fields for first name and last name. You want to create a new field called "name" that combines these two fields. Here's how you can achieve this using a lambda:

  1. Run the Endpoint: First, run the endpoint to see the current list of player objects with separate first name and last name fields.
  2. Open the Lambda Editor: In your function stack, click on the "Lambda" option at the bottom. This will open the JavaScript editor.
  3. Paste the Lambda Code: Paste the following code into the editor:

javascript return { ...response, data: response.data.map(player => ({ ...player, name: `${player.firstName} ${player.lastName}` })) }

This code creates a new `name` field that combines the `firstName` and `lastName` for each player object.

  1. Save and Run: Save the lambda function and run the endpoint again. You'll now see the new `name` field added to each player object, combining the first and last name.

Capitalizing Names with Lodash

But what if you want to capitalize the first letter of each name? No problem! With lambdas, you can leverage JavaScript libraries like Lodash to accomplish this.

  1. Open the Lambda Editor: Open the lambda editor again.
  2. Update the Code: Modify the previous code to use the `capitalize` function from Lodash:

javascript const _ = require('lodash'); return { ...response, data: response.data.map(player => ({ ...player, name: `${_.capitalize(player.firstName)} ${_.capitalize(player.lastName)}` })) }

  1. Save and Run: Save the updated lambda function and run the endpoint. Now, the `name` field will display the first and last names with the first letter capitalized.

If you need a library that's not currently available in Xano, simply let us know, and we'll be happy to add it for you. We're committed to providing a flexible environment for you to enhance your Xano experience.

Using Lambdas in Higher-Order Filters

Lambdas aren't limited to the function stack. You can also use them with higher-order filters throughout Xano.

  1. Apply a Filter: Let's hide the lambda function from the previous example and apply a filter to the response.
  2. Open the Lambda Editor: In the filter menu, select the "Map" filter, which is a higher-order filter that supports lambdas.
  3. Paste the Lambda Code: Paste the following code into the lambda editor:

javascript const _ = require('lodash'); return { name: `${_.capitalize(this.name.firstName)} ${_.capitalize(this.name.lastName)}` }

This code performs the same capitalization and name combination as before, but within the context of the higher-order filter.

  1. Save and Run: Save the lambda function and run the endpoint. You should see the same results as before, with the capitalized names displayed.

Final Thoughts

Lambdas in Xano open up a world of possibilities for those who have some JavaScript knowledge. You can now blend the power of code with the simplicity of Xano's no-code approach. Whether you use lambdas in your function stack or within higher-order filters, you can supercharge your business logic and create more sophisticated applications.

Of course, if you prefer to stick to the pure no-code approach, Xano still has you covered. Lambdas are an optional tool for those who want to take advantage of them.

Thanks for reading, and happy coding (or no-coding)!

This transcript was AI generated to allow users to quickly answer technical questions about Xano.

Was this helpful?

I found it helpful

I need more support
Sign up for XanoSign up for Xano

Build without limits on a secure, scalable backend.

Unblock your team's progress and create a backend that will scale for free.

Start building for free