Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.

First time visiting? Here are some places to start:
  1. Looking for a certain topic? Check out the categories filter or use Search (upper right).
  2. Need support? Ask a question to our Community Support category.
  3. Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
  4. 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.

Two Array > Zip > Key, Value Mapping

Jonathan_HutchinsonJonathan_Hutchinson Member Posts: 91 PRO
I'm trying to carefully approach this one as I don't see an elegant way through at first glance. Given two arrays a and b, sized n, how could you create from them a map like so:

{ a[0]: b[0], a[1]: b[1] }

Comments

  • EvanReeseEvanReese Member, Mentor Posts: 2,144 ✭✭✭✭✭
    edited June 2023
    Is your goal to create the map from the arrays or make the arrays from a map or what? Does this do anything you're after?

            const a = [12,15,2,5];
            const b = [3,5,7,9];
            const count = size(a);
            var theMap = {};
            
            for (var i = 0; i < count; i += 1)
            {
                theMap[a[i]] = b[i];
            }

    Evan Reese
  • Jonathan_HutchinsonJonathan_Hutchinson Member Posts: 91 PRO
    Yeah that does the job very nicely! I wasn't sure if I was misreading the API for a shorthand of it in method overloads. But this is short enough!
  • EvanReeseEvanReese Member, Mentor Posts: 2,144 ✭✭✭✭✭
    Cool! good opportunity to write a function if this is something you need to do often.
    Evan Reese
Sign In or Register to comment.