1

How to Iteratively Add Items to a New Array

I'm new to Ninox (less than a month of use), so if this is common knowledge I apologize. However, I feel compelled to post this because of the incredibly steep learning curve of Ninox. I've spent hours in the forums and in the database figuring out how to do basic things like data manipulation. (I come from a background in Basic, C++, Swift, and Python, so it's not like I'm new to coding in general.) The abysmal quality of the official documentation doesn't help either.

Anyway, here's a quick and easy way to iteratively add items to a new blank array. Put the following code snippets in a button to test/manipulate.

Simple version, array of numbers:

let finalArray := [number(void)];
for i from 1 to 10 do
    let currentArrayItem := [i];
    finalArray := array(finalArray, currentArrayItem)
end;
finalArray := slice(finalArray, 1, length(finalArray));
alert(finalArray)

On line 1, set the finalArray variable as an array of type number(void). This is important because the array() function (which joins two arrays) must be given arrays of the same type. You cannot declare the following: 

let finalArray := [];

There is no type, and Ninox cannot infer a type when using array().

You may notice that if you remove line 6, you'll end up with a finalArray[] that has a blank first value. This is because of the way we initialized finalArray. The slice() function serves to remove the first null value.

In that vein, if you're working with text instead of numbers:

let finalArray := [text(void)];
for i from 1 to 10 do
    let currentArrayItem := [text(i)];
    finalArray := array(finalArray, currentArrayItem)
end;
finalArray := slice(finalArray, 1, length(finalArray));
alert(finalArray)

Pay attention to line 3: [text(i)]. The brackets go around the text() declaration, not the other way around. All text you want to add to the array item must go inside the text() declaration. Here's a slightly more complex example from my own database (without context):

let items := [text(void)];
let c := 1;
for service in n.'Itemized Receipt' do
    let x := [text(c + " - " + service.Code + " - " + service.'Service Name')];
    items := array(items, x);
    c := c + 1
end;
items := slice(items, 1, length(items));
let allItems := join(items, "
");
i.('Itemized Services' := allItems);

Notice:

  • Line 2: I implement an index variable.
  • Line 4 : I create a long text string with multiple calculated values.
  • Lines 9-11: I join the entire final array into one text string, separated by newline characters, and then paste it into a multi-line text box.

Hopefully someone new to Ninox can benefit from this and save time. If you know of any other array manipulation tricks, feel free to post or link them below so there's a sort of central repository of Ninox array related information.

4 replies

null

Content aside

  • 1 Likes
  • 11 mths agoLast active
  • 4Replies
  • 509Views
  • 4 Following