cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
mdennis
Frequent Visitor

Object keyed by variable

Is there any correct way to do something like:

 
Set(obj,{1:"one",2:"two",3:"three"})
Set(k,2)
 
obj[k] (would return "two")
 
(Please dont tell me the only way is to make a series of if statements)
1 ACCEPTED SOLUTION

Accepted Solutions
Community Support
Community Support

Re: Object keyed by variable

Hi @mdennis ,

Do you want to use the obj[k] formula to reference the value from your obj variable?

 

Based on the formula that you mentioned, I think there is something wrong with it. Firstly, the obj[k] formula is not supported within PowerApps currently.

 

In addition, the obj variable that you set within your app is an Object rather than an array. The obj[k] formula is used to reference values from an array.

 

I have made a test on my side, please take a try with the following workaround:

Set the OnStart property of the App control to following (initialize your obj variable):

ClearCollect(obj, {Id:1, Value:"one"}, {Id:2, Value:"two"}, {Id:3, Value:"three"})

Set the OnSelect property of a Button to following (press this button to set the k variable):

Set(k, 2)

Add a Label control within your app, set the Text property to following:

LookUp(obj, Id = k, Value)

More details about the LookUp function, please check the following article:

LookUp function

 

Best regards,

Community Support Team _ Kris Dai
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

3 REPLIES 3
jhall Continued Contributor
Continued Contributor

Re: Object keyed by variable


@mdennis wrote:

Is there any correct way to do something like:

 
Set(obj,{1:"one",2:"two",3:"three"})
Set(k,2)
 
obj[k] (would return "two")
 
(Please dont tell me the only way is to make a series of if statements)

LookUp() is the path, but you might need to alter your initial Collection slightly.  For example:  

  • ClearCollect(obj,{id:1,value:"one"},{id:2,value:"two"],{id:3,value:"three"})
  • Set(k,2)
  • LookUp(obj,id=k,value)   
    • (returns "two")

It's possible this could be done w/o the Collection mod but I haven't tried it.

Dual Super User
Dual Super User

Re: Object keyed by variable

To add to @jhall 's answer. The syntax you were trying to use is an array.  PowerApps doesn't support an Indexed array.  But as was suggested you can easily make an indexed collection of objects and then access those objects using the index.



-------------------------------------------------------------------------
If I have answered your question, please mark your post as Solved.
If you like my response, please give it a Thumbs Up.
Community Support
Community Support

Re: Object keyed by variable

Hi @mdennis ,

Do you want to use the obj[k] formula to reference the value from your obj variable?

 

Based on the formula that you mentioned, I think there is something wrong with it. Firstly, the obj[k] formula is not supported within PowerApps currently.

 

In addition, the obj variable that you set within your app is an Object rather than an array. The obj[k] formula is used to reference values from an array.

 

I have made a test on my side, please take a try with the following workaround:

Set the OnStart property of the App control to following (initialize your obj variable):

ClearCollect(obj, {Id:1, Value:"one"}, {Id:2, Value:"two"}, {Id:3, Value:"three"})

Set the OnSelect property of a Button to following (press this button to set the k variable):

Set(k, 2)

Add a Label control within your app, set the Text property to following:

LookUp(obj, Id = k, Value)

More details about the LookUp function, please check the following article:

LookUp function

 

Best regards,

Community Support Team _ Kris Dai
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

Helpful resources

Announcements
MBAS Gallery 2020

MBAS Gallery 2020

Watch Microsoft Business Applications Summit sessions on-demand.

Attending MBAS? Claim your badge

Attending MBAS? Claim your badge

Whether you’re streaming the digital event live, or watching on-demand, claim your attendee badge to sport on your profile.

‘Better Together’ T-Shirt Contest – Winner Announced!

‘Better Together’ T-Shirt Contest – Winner Announced!

And the winner is...

firstImage

New Ranks and Rank Icons released on April 21!

The time has come: We are finally able to share more details on the brand-new ranks coming to the Power Apps Community!

Power Platform 2020 release wave 1 plan

Power Platform 2020 release wave 1 plan

Features releasing from April 2020 through September 2020

‘Better Together’ Integration Forum Launch

‘Better Together’ Integration Forum Launch

We're launching a how-to forum where you can learn about how Power BI integrates with other Power Platform products.

Emergency Response & Monitoring Solution

Emergency Response & Monitoring Solution

Microsoft has created a regional government emergency response and monitoring solution that is built on Power Apps and Power BI.

Top Solution Authors
Top Kudoed Authors
Users online (5,359)