How to use all the new array actions

There are 14 new SC actions for arrays which are awesome but I urgently need a steer on how to use each of them. The main ones to create the array, insert data into it, read data back, etc. are all straightforward.

But I want to insert into an array if the value isn’t already in there so I’m assuming the one to use is ‘Array List Contains’ but I just can’t see how it should be used. I would have expected a list of variables and then the option to put a value (static or dynamic) into one or more of those but it just give a single field.

So, how should it be used. And, before that, is this action designed to do what I’m hoping it will?

Until the full documentation is available, could you just give a quick rundown on what all the actions do and how they should be used?

3 Likes

I would advise to just put simple values in the array like numbers or strings. You can also store object in it, but the contains does not work for them. The reason is that everything is stored by value and not by reference. This means that comparing 2 objects which have the same properties but are not the same instance will be seen as different, that is why contains can’t correctly detect objects, with strings and numbers it works fine. The contains action uses Array.prototype.includes() which uses Strict equality (===) for comparing values.

Create Array List : Create a new array
Get Array List Values : Returns the array (for when you want to output it to the client)
Array List Size : Returns the number of items in the array array.length
Get Array List Index : Get the value on a specific index array[index]
Add in Array List : Add new item to the array array.push(value)
Add All in Array List : Add multiple items to the array array.concat(values) (you can also see this as a concat joining 2 arrays together)
Set Array List Index : Set a value at a specific index array[index] = value (for updating a specific index/value)
Remove Array List Item : Remove a specific value (this has the same problem as the contains, works fine with strings and numbers but not with objects)
Remove Array List At Index : Remove value at a specific index
Clear Array List : Remove all values from the array creating an empty array
Sort Array List : Sorting the array array.sort()
Array List IndexOf : Lookup index of a value array.indexOf(value) (again same problem as described above)
Array List Contains : Check if array contains a value array.includes(value)
Array List is Empty : Check if array is empty array.length === 0

We can perhaps change the behavior of how values are being compared since it can be strange for non-programmers. Noticed that a few users had problems with it, so changing that behavior would probably a good thing.

3 Likes

Thanks Patrick, that is super helpful.

Can I explain what I’m trying to do so you can advise which actions should be used to achieve it?

I have an array with just two fields - itemid and supplierid. Both are numbers. I have filled them with data and they work fine. I want to split the data based on the supplierid. Eg. The itemid will always be unique but the supplierid won’t be. It’s so I can send an email to each supplier containing all the items which are linked to them.

My thinking was to create another array which contains just one field: supplierid. I would go through the original array using the repeat action and then check if the supplierid is already in the new array. If it is, do nothing and move on to the next one. If it’s not, insert it. Then I can do a repeat on that new array to process each supplier.

I hope I’ve explained it well. Please let me know how I should construct it.

Thanks again.

Wouldn’t the Group By formatter just do the trick. Create a Set Value action, select the array with that contains the records with itemid and suplierid, then click the formatter icon and add the Group By formatter, for the Property parameter select the suplierid. You will now have an object with suplierids as key and the items grouped by suplier.

1 Like

I’ve spent a chunk of today working through this but I’m wondering if there’s a bug.

I’ve got my array which has the two vars in it. I’ve then added the Set Value, selected the array and used the Group By formatter:

listSuppliers.groupBy('supplierid')

I then added the repeat and set it to the Set Value which is valSuppliers but that gives me an error:

message : "Array to string conversion"

So I tried setting the repeat to use valSuppliers.data instead. That fixed the error but the data is null.

My Set Value is an array but when I click the Define Schema it puts all the fields in twice. And if I go back to it they are added again.

It all seems to be behaving oddly. I was expecting to set the repeat using the Set Value and then being able to add the Output Fields but it just says Empty list.

There’s either a bug somewhere which is causing the fields to not work as they should or I’m doing something wrong. And I am expecting it to be the latter!

What exactly are you trying to do with the setvalue step then?
Once you follow patrick’s advice you will end up with a setvalue step containing an object with the items grouped by suppliers:

so what’s the issue you are having?

Thanks for your assistance, @Teodor. I have that showing as it should. I now want to do a repeat on that so I can send an email to each supplier with the items listed in the email. So my repeat builds up the list and then ends with the Send Email action.

I’ve created the repeat and set it to output but it’s always just null.

What is null?
And you can just skip the set value step in your case and use the same expression in a repeat step … this will repeat through the supplier id. Use the send mail step there.

1 Like

Have you set the repeat output filter to Exclude? If you choose include, you have to add the fields to include. Exclude and empty fields will output everything

Yep. Good call but I had that. And it lets me select the Output Fields fine so that part is working as expected.

If you loop over an object use the $key and $value to access its keys/values.

I’m so sorry, this must be mega frustrating for you all! I know it’s a simple thing I’m trying to do, I just can’t get it right!

This is what I currently have:

But that is giving me the ‘Array to string conversion’ error.

How should I change the loop?

Which is “that”? What steps do you have inside the repeat?

Doh! I’ve been a fool. (None of you are surprised, are you!!!)

I was thinking it was the repeat itself which was causing the error but I had a query in the loop which turns out to be the cause.

HUGE thanks to everyone who’s helped my cause. I am hoping I can now get this done.

I continue to be amazed at Wappler, the team and this incredible community. :slight_smile:

Sorry @patrick . How do I use the $key and $value?

Why not start with simple debug steps?

Inside your repeat add two set value steps. Give one of them a value of {{$key}} then give the other one a value of {{$value}} and see what they return.

1 Like

Genius! {{$key}} is what I needed and it now works perfectly.

Thank you again. :slight_smile:

1 Like

So reading through this thread I’m thinking what I’m looking for is not possible or I’m missing something with the array actions.

I’ve converted a php project to NodeJS and in my php project I created a custom formatter that would take the output of the following query
SELECT column_name,column_default,data_type,column_type,column_key FROM information_schema.columns WHERE TABLE_SCHEMA='mydb' AND TABLE_NAME='users'
and convert it into an associative array where the column_name is the key and the column_default is the value. So for example the result for a users table would be something like array(‘first_name’=>’ ‘,‘last_name’=>’ ',‘status’=>‘Active’)
I used this for the form values when creating a new record, the defaults are used.

Of course, I can’t use PHP in the Node project so I’m wondering what the best native wappler way to accomplish this array is. I thought the array actions would be it but I don’t see a way to push an $array[‘key’] = value into an array list. From my understanding you can either replace an index value or push values, but not a key=>value pair. Is that correct?

Hope this makes sense.

Thanks in advance,
Twitch