How Do I Merge Two Sub Arrays in a Repeat?

When creating sub-arrays, you can want them with an and or an or function.

I can do the and function with multiple .where() conditions

my_array.where(`field1`, 1, "==").where(`field2`, 1, "==")

So now I want to do the or function - so basically to merge two arrays:

my_array.where(`field1`, 1, "==") MERGED WITH my_array.where(`field2`, 1, "==")

How do I do that?

While this kind of thing can be done during database access, I need it for Data Store access.

Bump!

Hello Antony,
What is your idea? What are you trying to achieve exactly?

I want a sub array of my_array where field1==1 OR field2==1.

So if I have:

Name field1 field2
==================
Fred    1     1
John    1     0
Jane    0     1
Mary    0     0

I want an array with Fred, John and Jane.

The .where(field1==1).where(field2==1) structure will only give me Fred.

It is the basis of some very complex search functions I need to carry out on my contacts Data Store.

So why not try doing:

my_array.where(`field1`, 1, "==") || my_array.where(`field2`, 1, "==")
1 Like

oh fab, so || works for arrays too?

Well, not sure what you mean it works for arrays, but of you want to return the values you mentioned in your previous post, the expression should be:

{{my_array.where(condition1) || myarray.where(condition2)}}

and as your explanation is not really clear, and as i don’t understand what are you trying to achieve exactly and where your data comes from and how - please try this and let me know if it works.

I’m sorry, but {{my_array.where(condition1) || myarray.where(condition2)}} will not work. It will always return the first filtered array. At this moment it is not possible to do an OR unless you write your own custom formatter.

1 Like

Thanks for your help in this folks!

@patrick, is there any documentation on writing your own custom formatter?

There are a few threads with examples of using custom formatters.

There are a couple of alternative methods to achieve what you want (I think). You could create a custom query using CASE WHEN, or you could use Query Builder and add a virtual column to your table (if you’re using MySQL anyway).

Is on the server-side or client-side that you need it?

For client-side formatters:

You could try my_array.filter('field1 == 1 || field2 == 1'), it is a formatter to filter an array that isn’t available in the UI.

A custom formmatter looks like:

dmx.Formatters('array', {
  my_filter: function(arr) {
    return arr.filter(function(row) {
      return row.field1 == 1 || row.field2 == 1;
    });
  }
});

usage: my_array.my_filter().

3 Likes

Thanks for your help folks!

It is all totally on the client side, using data from a Data Store, so no MySQL involved.

I’ll give that a go Patrick!