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.

FeatureScript makeArray with index value

RootentityRootentity Member Posts: 21 ✭✭
I would like a clean way to fill an array on creation with reference to the current cell being filled without using a for loop to do it.

var myValue = 10;
const myArray = makeArray(10, myValue * i);

with the result being [0,10,20,30,40,50,60,70,80,90]


  • kevin_o_toole_1kevin_o_toole_1 Onshape Employees, Developers, HDM Posts: 459
    edited April 2017
    Your case is what the math module's range functions are designed for (the function is optimized to be faster than doing it with a loop yourself).

    If you need numbers that aren't linear, you can call mapArray on a linear range. e.g.
    const myArray = mapArray(range(0, 10), function(i) { return cos(i * 30 * degree) });

    The non-linear case is probably slower than a simple for loop – but profiling would say for sure.

Sign In or Register to comment.