Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.
First time visiting? Here are some places to start:- Looking for a certain topic? Check out the categories filter or use Search (upper right).
- Need support? Ask a question to our Community Support category.
- Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
- Be respectful, on topic and if you see a problem, Flag it.
If you would like to contact our Community Manager personally, feel free to send a private message or an email.
Is map order arbitrary?
EvanReese
Member, Mentor Posts: 2,135 ✭✭✭✭✭
When I have an empty map and I add new entries, does it add them in any particular order? If not, can I force it to? For example if I do this:
var myMap = {}; myMap.one = 1; myMap.two = 22; myMap.three = 333; print(myMap);The printed result might look like this, which is out of order. I hoped it would always add the new one at the end.
{"two" : 22, "one" : 1, "three" : 333}
Evan Reese
0
Answers
Where each item in the map is assigned a random hash code that can be looked up quickly given the key.
So, it's position of an object in the map is irrelevant to function and becomes unsorted.
(103) 10. Dictionaries - YouTube
This video will teach you everything you want to know about dictionaries, but you can skip to about 50min, that's when it gets out of the theory and into a more practical view of it.
I'm not sure how Onshape handles the specifics, but I would assume the hash code is generated in the same way as the internal IDs in featurescript.
Or I could be completely wrong.
Iteration over FS maps should happen in key order. I expect to (and do) see
{ one : 1 , three : 333 , two : 22 }