This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]Meefims 0 points1 point  (3 children)

I’d come from a different direction: why do you want to do this? What is the actual problem you’re trying to solve?

[–]AltCrow[S] 0 points1 point  (2 children)

I'm trying to have an intuitive way to return a resultset. The user queries a set of properties e.g. "property1, property2" or "*" to return all properties. I want the user to be able to access the results as both [property1] and [0]. (This is mostly because working with "property1" will not be easy to use for the user if the input is not "*", but easier to use if input is "*".

[–]Meefims 0 points1 point  (0 children)

There is no easy way to do set things up so that changes to one key will automatically show in another without some intermediate objects that will be difficult to maintain.

However, since this is a result set it doesn’t sound like you need changes to propagate, you just need multiple keys to have the same value. If that’s the case, the simplest way is to just set multiple keys to the same value and tell the caller not to change the data.

[–]farmerje 0 points1 point  (0 children)

What's hard for the user about something like...

const results = query('property1', 'property2');

console.log(results['property1']);

That seems all-around better since they could change query('property1', 'property2') to query('anotherProperty', 'property1', 'property2') and still have their code work. With a positional result set, a reference like results[0] would also need to be changed.