Find the First or Last Index Position of Array Item with JavaScript Code Step

Find the First or Last Index Position of Array Item with JavaScript Code Step
Userlevel 7
Badge +14

📌  We do our best to ensure that the code in these articles works as advertised, but please be aware that Zapier Support does not officially help with code steps due to their advanced nature. Feel free to comment on the article or ask in the Zapier Community, where we have code-savvy users.

The Purpose

Find the first and/or last index position of an item in an array.

  • Use indexOf() to get the first index position for a value.
  • Use lastIndexOf() to get the last index position for a value.

NOTE: Both methods will return -1 if the value is not found.

How to Configure

Copy the Code

let A1 = inputData.A1.split(","); // splits by the comma delimiter to create an array
let Find = inputData.Find; // value to find in the array

let IndexFirst = A1.indexOf(Find); // finds the first index position
let IndexLast = A1.lastIndexOf(Find); // finds the last index position

output = [{IndexFirst, IndexLast, A1}];

The Results

NOTE: Index positions start at 0!

Contribution by Troy Tessalone

Troy is a Certified Zapier Expert who automates workflows with no-code and low-code apps to help clients save time and make money.


This post has been closed for comments. Please create a new post if you need help or have a question about this topic.