Arrays in back end

Hi everyone!

I am having problems understanding how to do certain array manipulations in a server action (i.e., the back-end side) and since I could not find a document or postings to guide me, any help you can provide here will be great!

Assume that in my server action API, I’ve done the following:

  1. Defined the variables TempArray and FinalArray as arrays using the “Core Actions > Set Value” step, set their Global Names and initialized them to null. For the sake of argument, let’s say that the structure of these arrays ismade of the text “ItemName” and the numeric “ItemAge”.

  2. Run a custom query against my database and obtained the array ResultSet. Assume that the query returned the fields “FullName” and “DateOfBirth”

  3. Used the step “Core Actions > Repeat” to cycle through the $values of ResultSet, and did some string and date operations on them to set the values of the variables RepeatName and RepeatAge:

My questions are:

A) ADD ONE ITEM TO THE END AN ARRAY
How do I add a new item to TempArray within an iteration of my repeat loop and use the resulting RepeatName and RepeatAge calculated in the current iteration to set the values of ItemName and ItemAge to the new item in my TempArray?

B) CREATE A “CLEAN” ARRAY AFTER THE REPEAT
How do I set the array FinalArray to contain a clean version with all the items that TempArray would have at the end of my repeat loop? Is it simply through a set value step or do I have to add a .split(’,’) to TempArray?

C) UPDATE AN ARRAY ITEM
Assume that after running the steps above, my FinalArray has 200 items. How to I update a specific item in it? To be more specific, What would be the syntax to do something like what’s below in a server action?:

FinalArray[14].ItemAge = “42”

D) MISCELLANEOUS

  • What does the formatter function “Collections > Join” do to an array? What is the difference between this and the action “Data Transformation > Join” ?
  • What does the formatter function “Collections > Flatten” do to an array?

Many thanks and Happy Holidays and Happy New Year to all!
Alex

PS:

  • To give focus the questions, the example given above is much simpler than what I need to do. In real life, I have to do nested repeats with multiple arrays, and a much more complex logic than what’s shown here, so to avoid mudding the waters, I reduced it to the syntax that I need to use.
  • I’m using NodeJS but I think the same questions would apply to all other server modes.

Hi Alex,
Not going too deep into an explanation, we have found RunJS to be the best solution when doing any data manipulation on server side, that is out of scope of Wappler.
Earlier, we used to either create formatters or modules for such specific needs, but now we just use this.

Thank you Sid for the suggestion.
I read from your suggestion (and a few postings that I found searching for Run JS in the Forum), that out-of-the-box Wappler can’t do what I’m trying to do.

I think I’d be a bit over my head (I can barely spell JavaScript), but I’m willing to try your suggested approach anyway.

What do I need to do to have the action called Run JS in my project? I imagine that I’d have to install something, but I don’t know what should be installed where and how.

Is there any instruction on how to do that for someone who has never done it?

Many thanks,

Alex

Adding it to the project is same as adding any other extension - just copy the two files in modules folder.
You can find more details here: About the Wappler Extensions category

It will be a learning curve definitely, but since you already have an idea around what an array and an object is, it should be easier for you.

thank you but I have two questions:

  1. Where are the two files are you referring to?

  2. The article you reference talks about a folder called extensions/server_connect/modules
    That folder does not exist in my project. Where exactly should it be created?

Sorry for all these questions, but I’ve never done this before.

Many thanks in advance!

Alex

There’s literally just two files in the Git project.

Be sure to copy from master branch.

Root of your project.

Thank you so much, Sid.
With your instructions, I managed to install the module and now I have a working RunJS step in my project.
I replicated the example posted by JonL to concatenate arrays, adjusted it to use the keyword “return”, and everything worked as expected.

I’m now in a position to continue thanks to you and this is really appreciated!!

Alex

1 Like

server side array manipulation is still something I’d like to see put into wappler. I’m not crazy about manipulating arrays on the client side as you open yourself up to vulnerabilities and unintended manipulation of data.

Glad you found a work around.