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.

Looping through a Const list

Nath_McCNath_McC Member Posts: 127 PRO
edited October 2022 in FeatureScript
Hi All,

I have a list of values in a const list 'arraySize" and wanted to know if it is possible to have a value loop through them.
My example is below, and the value I would be looking to show is:
        var myDiameter = 13 * millimeter
        var actualDiameter = "This should be the value from the the const table, e.g. 9/16″  (14.28mm)
How would I go about having it loop through the below list to find the value it is less than? The value I need to get, I believe, would generally be from a getLookupTable(arraySize, definition.actualdiameter).dwgValue however, the myDiameter value is not from user input as it is a calculation from the code based on the bounding box size.

Thank you in advance.

The list I'd like the loop through is below:
export const arraySize = { "name" : "stockSize", "displayName" : "Stock Size", "entries" : { "3" : { "metricValue" : 3, "dwgValue" : "3mm" }, "3.17" : { "metricValue" : 3.17, "dwgValue" : "1/8″ (3.17mm)" }, "4" : { "metricValue" : 4, "dwgValue" : "4mm" }, "4.73" : { "metricValue" : 4.73, "dwgValue" : "3/16″ (4.73mm)" }, "5" : { "metricValue" : 5, "dwgValue" : "5mm" }, "6" : { "metricValue" : 6, "dwgValue" : "6mm" }, "6.35" : { "metricValue" : 6.35, "dwgValue" : "1/4″ (6.35mm)" }, "7.93" : { "metricValue" : 7.93, "dwgValue" : "5/16″ (7.93mm)" }, "8" : { "metricValue" : 8, "dwgValue" : "8mm" }, "9.53" : { "metricValue" : 9.53, "dwgValue" : "3/8″ (9.53mm)" }, "10" : { "metricValue" : 10, "dwgValue" : "10mm" }, "11.11" : { "metricValue" : 11.11, "dwgValue" : "7/16″ (11.11mm)" }, "12" : { "metricValue" : 12, "dwgValue" : "12mm" }, "12.7" : { "metricValue" : 12.7, "dwgValue" : "1/2″ (12.7mm)" }, "14.28" : { "metricValue" : 14.28, "dwgValue" : "9/16″ (14.28mm)" }, "15.87" : { "metricValue" : 15.87, "dwgValue" : "5/8″ (15.87mm)" }, "16" : { "metricValue" : 16, "dwgValue" : "16mm" }, "18" : { "metricValue" : 18, "dwgValue" : "18mm" }, "19.05" : { "metricValue" : 19.05, "dwgValue" : "3/4″ (19.05mm)" }, "20" : { "metricValue" : 20, "dwgValue" : "20mm" }, "22" : { "metricValue" : 22, "dwgValue" : "22mm" }, "22.23" : { "metricValue" : 22.23, "dwgValue" : "7/8″ (22.23mm)" }, "24" : { "metricValue" : 24, "dwgValue" : "24mm" }, "25" : { "metricValue" : 25, "dwgValue" : "25mm" }, "25.4" : { "metricValue" : 25.4, "dwgValue" : "1″ (25.4mm)" }, "28.58" : { "metricValue" : 28.58, "dwgValue" : "1 1/8″ (28.58mm)" }, "30" : { "metricValue" : 30, "dwgValue" : "30mm" }, "31.75" : { "metricValue" : 31.75, "dwgValue" : "1 1/4″ (31.75mm)" }, "34.93" : { "metricValue" : 34.93, "dwgValue" : "1 3/8″ (34.93mm)" }, "35" : { "metricValue" : 35, "dwgValue" : "35mm" }, "38.1" : { "metricValue" : 38.1, "dwgValue" : "1 1/2″ (38.1mm)" }, "40" : { "metricValue" : 40, "dwgValue" : "40mm" }, "41.28" : { "metricValue" : 41.28, "dwgValue" : "1 5/8″ (41.28mm)" }, "44.45" : { "metricValue" : 44.45, "dwgValue" : "1 3/4″ (44.45mm)" }, "45" : { "metricValue" : 45, "dwgValue" : "45mm" }, "47.63" : { "metricValue" : 47.63, "dwgValue" : "1 7/8″ (47.63mm)" }, "50" : { "metricValue" : 50, "dwgValue" : "50mm" }, "50.8" : { "metricValue" : 50.8, "dwgValue" : "2″ (50.8mm)" }, "53.98" : { "metricValue" : 53.98, "dwgValue" : "2 1/8″ (53.98mm)" }, "55" : { "metricValue" : 55, "dwgValue" : "55mm" }, "57.15" : { "metricValue" : 57.15, "dwgValue" : "2 1/4″ (57.15mm)" }, "60" : { "metricValue" : 60, "dwgValue" : "60mm" }, "60.32" : { "metricValue" : 60.32, "dwgValue" : "2 3/8″ (60.32mm)" }, "63.5" : { "metricValue" : 63.5, "dwgValue" : "2 1/2″ (63.5mm)" }, "65" : { "metricValue" : 65, "dwgValue" : "65mm" }, "69.85" : { "metricValue" : 69.85, "dwgValue" : "2 3/4″ (69.85mm)" }, "70" : { "metricValue" : 70, "dwgValue" : "70mm" }, "76.2" : { "metricValue" : 76.2, "dwgValue" : "3″ (76.2mm)" }, "80" : { "metricValue" : 80, "dwgValue" : "80mm" }, "82.55" : { "metricValue" : 82.55, "dwgValue" : "3 1/4″ (82.55mm)" }, "88.9" : { "metricValue" : 88.9, "dwgValue" : "3 1/2″ (88.9mm)" }, "90" : { "metricValue" : 90, "dwgValue" : "90mm" }, "95.25" : { "metricValue" : 95.25, "dwgValue" : "3 3/4″ (95.25mm)" }, "100" : { "metricValue" : 100, "dwgValue" : "100mm" }, "101.6" : { "metricValue" : 101.6, "dwgValue" : "4″ (101.6mm)" }, "110" : { "metricValue" : 110, "dwgValue" : "110mm" }, "120" : { "metricValue" : 120, "dwgValue" : "120mm" }, "127" : { "metricValue" : 127, "dwgValue" : "5″ (127mm)" }, "130" : { "metricValue" : 130, "dwgValue" : "130mm" }, "140" : { "metricValue" : 140, "dwgValue" : "140mm" }, "142.4" : { "metricValue" : 142.4, "dwgValue" : "6″ (142.4mm)" }, } };



Tagged:

Comments

  • _anton_anton Member, Onshape Employees Posts: 410
    Something like this, if I understand the ask right? https://cad.onshape.com/documents/2b6da5e47387308bdd243143/w/af22172ac903e5a92e494e46/e/7201b9d3b33162bcc59aed32

    This assumes the "metricValue" field is in millimeters.

    I would ideally start off with arraySize.entries as a map indexed by numbers (rather than stringified numbers like "142.4"). Then iteration through the map would happen in that order and it's a simple for...in loop to find the first value greater than your input.
  • Nath_McCNath_McC Member Posts: 127 PRO
    Hi @_anton,

    You are correct that the metricValue is millimeters.

    That I've what I was looking for, Thank you for the working example.

    My only question is it seems that "myLength" is 4.1mm, but it has picked the "1 5/8″ (41.28mm) , metricValue : 41.28", where really it should actually be the "3/16″  (4.73mm) , metricValue" : 4.73"
  • _anton_anton Member, Onshape Employees Posts: 410
    Nath_McC said:
    My only question is it seems that "myLength" is 4.1mm, but it has picked the "1 5/8″ (41.28mm) , metricValue : 41.28", where really it should actually be the "3/16″  (4.73mm) , metricValue" : 4.73"
    Hmm, I'm seeing the expected results (former for 4.1 *cm*, latter for 4.1 *mm*).

    Though I did not test my code very thoroughly. :P
  • Nath_McCNath_McC Member Posts: 127 PRO
    I now see that. It works brilliant, and I should be able to integrate it with the rest of my code. Thank you!
Sign In or Register to comment.