cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
sloanad2
Regular Visitor

Select each index from 2D array.

I have a 2D array, and I'm trying to use select to build a separate array using index 3 of each internal array (e.g., given starting array is [['An', 'Example', 'Array', 'Here'], ['But', 'It', 'Won't', 'Work']], I want ['Here', 'Work']). However, any index other than 0 will give me this error:

The evaluation of 'query' action 'where' expression '@item()[1]' failed: 'The template language expression 'item()[1]' cannot be evaluated because array index '1' is outside bounds (0, 0) of array.

I can check the length of each array just fine and index 0 is always correct so this really doesn't make any sense to me, and I'd appreciate any help. I'm also trying to avoid standard loops as they GREATLY reduce performance. Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
sloanad2
Regular Visitor

Really I needed an array of each array within a 2D array at index X. X is variable and the size of the arrays are variable as well which is why I needed a dynamic solution. Although it's not graceful, I have a solution through trimming/filtering my arrays and creating a new 2D array with createArray(item[X],...) along with all the other inputs I require. This leaves it in a form that allows me to use the data more effectively. This wouldn't work before as mentioned since attempting to call item[X] where X != 0 caused issues, I still don't fully understand why, and that's why I came here, but I've worked around it.
I've moved on to another, more complicated case, but I won't get into that here, thanks for the help anyway.

View solution in original post

5 REPLIES 5
ekarim2020
Super User
Super User

Hi @sloanad2 ,

 

Try the following:

Snag_204fb734.png

[
  [
    "An",
    "Example",
    "Array",
    "Here"
  ],
  [
    "But",
    "It",
    "Won't",
    "Work"
  ]
]

 

Snag_20507dfe.png

createArray(variables('varArray')?[0]?[3], variables('varArray')?[1]?[3])

 

Snag_205165ce.png

 


Ellis
____________________________________
If I have answered your question, please mark the post as Solved.
If you like my response, please give it a Thumbs Up.

sloanad2
Regular Visitor

@ekarim2020 Unfortunately I need this solution to work dynamically so adding each one by one isn't going to work for me.

 

Edit: Also my method of using select should work, it does in a test environment, but for some reason it's not working in the actual flow, where the dataset is significantly larger, although I'm not sure if that's the source of the issue.

Hi @sloanad2 ,

 

Please provide further details for the rule you need in order to generate the third array.  From your original post it seems that you need the last item from each of the nested arrays. If this is the case, you can use the following expressions:

 

createArray(last(first(variables('varArray'))), last(last(variables('varArray'))))

  

Snag_22839a00.png

Please post extracts from your working flow, including more sample data. Are you using the same sample data to test the flows in both your Test and Production environment?

 


Ellis
____________________________________
If I have answered your question, please mark the post as Solved.
If you like my response, please give it a Thumbs Up.

 

 

grantjenkins
Community Champion
Community Champion

Select will iterate through each of the inner arrays, so all you need to do is have the following:

 

item()[3]

 

grantjenkins_0-1669116800955.png

 

This is using an array variable with any number of inner arrays. You just need to ensure you have at least 4 items in each.

grantjenkins_2-1669116942137.png

 

If that's what you already have and it's not working, then I'd be looking at your array to see if it's malformed in some way.



----------------------------------------------------------------------

If I've answered your question, please mark the post as Solved.

If you like my response, please consider giving it a Thumbs Up.
sloanad2
Regular Visitor

Really I needed an array of each array within a 2D array at index X. X is variable and the size of the arrays are variable as well which is why I needed a dynamic solution. Although it's not graceful, I have a solution through trimming/filtering my arrays and creating a new 2D array with createArray(item[X],...) along with all the other inputs I require. This leaves it in a form that allows me to use the data more effectively. This wouldn't work before as mentioned since attempting to call item[X] where X != 0 caused issues, I still don't fully understand why, and that's why I came here, but I've worked around it.
I've moved on to another, more complicated case, but I won't get into that here, thanks for the help anyway.

Helpful resources

Announcements
Power Automate News & Announcements

Power Automate News & Announcements

Keep up to date with current events and community announcements in the Power Automate community.

Community Calls Conversations

Community Calls Conversations

A great place where you can stay up to date with community calls and interact with the speakers.

Power Automate Community Blog

Power Automate Community Blog

Check out the latest Community Blog from the community!

Top Solution Authors
Users online (6,052)