web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / How to find the index ...
Power Automate
Unanswered

How to find the index of matched element in an array

(0) ShareShare
ReportReport
Posted on by
In power automate, I found that there only exists indexOf function for string and no similar function for array.
Is there anyway to extract the index number of an array if element matches?
 
For example, I have an array variable named varCityArray which holds value like 
[
"New York",
"London",
"Paris",
"Tokyo"
]
 
If I want to search for "Paris", it should return 2.
Categories:
I have the same question (0)
  • stampcoin Profile Picture
    5,153 Super User 2026 Season 1 on at
    If you want to use the index value, probably loop each item of the array to get it.
    Loop until 
    variables('myArray')[variables('counter')] ='Paris'
     
  • Chriddle Profile Picture
    8,691 Super User 2026 Season 1 on at
    I'm pretty sure there's a better way, but for now:
    Compose
    The array must not be empty.
     
    Select
    From:
    range(0, length(outputs('Compose')))
    Map:
    if(
    	equals('Paris', outputs('Compose')[item()]),
    	item(),
    	-1
    )
    Compose_2
    max(body('Select'))
     
     
  • Chriddle Profile Picture
    8,691 Super User 2026 Season 1 on at
    This also works:
     
    if(
    	contains(
    		outputs('Compose'),
    		'Paris'
    	),
    	if(
    		equals(
    			first(outputs('Compose')),
    			'Paris'
    		),
    		0,
    		if(
    			equals(
    				last(outputs('Compose')),
    				'Paris'
    			),
    			sub(
    				length(outputs('Compose')),
    				1
    			),
    			length(
    				split(
    					first(
    						split(
    							join(outputs('Compose'), '-|-'),
    							'-|-Paris-|-'
    						)
    					),
    					'-|-'
    				)
    			)
    		)
    	),
    	-1
    )
     
  • Chriddle Profile Picture
    8,691 Super User 2026 Season 1 on at
    Or this:
    if(
    	contains(
    		string(outputs('Compose')),
    		'"Paris"'
    	),
    	length(
    		json(
    			concat(
    				'[',
    				slice(
    					string(outputs('Compose')),
    					1,
    					sub(
    						indexOf(
    							string(outputs('Compose')),
    							'"Paris"'
    						),
    						1
    					)
    				),
    				']'
    			)
    		)
    	),
    	-1
    )
     
  • Chriddle Profile Picture
    8,691 Super User 2026 Season 1 on at
    Or this:
    if(
    	contains(
    		outputs('Compose'),
    		'Paris'
    	),
    	sub(
    		length(
    			xpath(
    				xml(
    					addProperty(
    						json('{}'),
    						'Root',
    						addProperty(
    							json('{}'),
    							'Item',
    							outputs('Compose')
    						)
    					)
    				),
    				'//Item[not(preceding::*/text()="Paris")]'
    			)
    		),
    		1
    	),
    	-1
    )
     
  • Chriddle Profile Picture
    8,691 Super User 2026 Season 1 on at
    And finally (what I like best):
    xpath(
    	xml(
    		addProperty(
    			json('{}'),
    			'Root',
    			addProperty(
    				json('{}'),
    				'Item',
    				outputs('Compose')
    			)
    		)
    	),
    	'count(//Item[following::*/text()="Paris" or ./text()="Paris"]) - 1'
    )
     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Users!

Kudos to our 2025 Community Spotlight Honorees

Congratulations to our 2025 community superstars!

Congratulations to the April Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Automate

#1
Vish WR Profile Picture

Vish WR 959

#2
Valantis Profile Picture

Valantis 872

#3
Haque Profile Picture

Haque 589

Last 30 days Overall leaderboard