Using the Unique filter on an array of arrays.

Options

Hi.

I'm looking through data that needs me to go through an array that looks like the below

[[a,b,c],[c,b,a],[a,b],[c,d],[d,a],[d,a],[c,a]]

and from this data lift unique groupings and count them so that we have

[a,b,c] = 2

[d,a] = 2

[a,b] = 1

[c,a] = 1

[c,d] = 1

I've used the unique filter on the main array hoping to get the unique combinations then count them afterwards, but I keep getting hit with an unknow error exception.

Is there something I'm doing that's wrong?

Thanks!

Answers

  • Lefteris - blupry.com
    Options

    Hello @enyarick,

    Regarding the error you are facing, it is possible that your main array does not have the correct format.

    To construct such an array on Xano you can you use the push filter on an array, like this:

    Then using the unique filter like this:

    And the uniqueArrays result will be as such:

    While this implementation doesn't directly returns what you need, it should allow you to resolve the error you mentioned.

    If you need any assistance regarding the further implementation, let me know!

  • enyarick
    enyarick Member
    Options

    Thanks for this @BUYNOCODEAPPS!.

    Challenge is here we have a repeated array at position 0 and 1. It's just the arrangement which is different.

    What I'm looking for is to have unique items only returned. Sorting the smaller arrays would definitely help. Let me try and see if that helps.

    The next stage will be to count how many times each unique array appears so if I can get part 1, then part 2 should be doable.

  • enyarick
    enyarick Member
    Options

    I'm testing using a for each loop where the array is iterated through and each sub-array is sorted and compared to a sorted array that exists. This also doesn't seem to work.

  • Lefteris - blupry.com
    Options

    Hello again @enyarick,

    In a pretty high level design, this is what I would probably do:

    The newArray should then be filtered for unique entries.

  • enyarick
    enyarick Member
    Options

    Thanks for this @Lefteris - Buynocodeapps!

    Unfortunately, this append filter doesn't seem to work inside a for each loop.

    [

    [a,b,c],

    [d,a],

    [a,b],

    [c,a],

    [c,d]

    ]

    The array looks like this and I'm iterating through an array with the intent each time to add an element to the array e.g. adding [c,b,a] to the end of this array so that it now looks like this

    [

    [a,b,c],

    [d,a],

    [a,b],

    [c,a],

    [c,d],

    [c,b,a]

    ]

    I'm able to do this from outside a for each loop using both add to end/beginning of array as well as append.

    But from inside the for each loop I'm getting nothing being added.

    Still experimenting.

    Thanks!

  • enyarick
    enyarick Member
    Options

    Thanks for this @Lefteris - Buynocodeapps!

    Unfortunately, append doesn't seem to work.

    [

    [a,b,c],

    [d,a],

    [a,b],

    [c,a],

    [c,d],

    ]

    I trying to use a for each loop to find an element from an array like the one above and compare it to the rest. If it exists, skip. If it doesn't add it to the unique array. I'm able to match the elements, but can't seem to add a new element to the "arrayofarrays" either using a push, or using an append.

    I've tried to update variable where I'm appending the new array. I've also tried adding to end/beginning of array.

    So if I want to add [c,b,a] to the above the result should be

    [

    [a,b,c],

    [d,a],

    [a,b],

    [c,a],

    [c,d],

    [c,b,a]

    ]

    Any ideas?

  • Lefteris - blupry.com
    Options

    Hi again @enyarick,

    Are you sure it doesn't work?

    The append should look something like this:

    Maybe you can add a screenshot of the debugger, so I can understand better what issue are you facing.