How should I combine array values?

Options

I have the following:

I want to replace userId:[3,14,2] with userIds: '3,14,2'.

Essentially I want to do a JOIN on the array.

I could have maybe 100 different JSON objects with arrays containing maybe 100 elements.

I can loop through all of the JSON objects in the result and then loop through all userIds appending them to a string variable. That is easy enough, but is that the best way to do this?

Could this be done with Lambdas? If so please provide some examples.

I would like to do the same with securityGroupId and securableEntityId.

Best Answers

  • pete letkeman
    pete letkeman Member
    edited November 2022 Answer ✓
    Options

    @Michael Udinski, sorry not exactly what I'm asking, let me try again.

    I have 100 records, each with an array that can have up to 100 values.

    I am NOT updating the database.

    I want to take each array and do an array.join (https://www.w3schools.com/jsref/jsref_join.asp) so that instead of an array of 100 values I have a single string with all of the values commas separated, and I want to keep the other values or the record as is.

    I CAN do this easily enough in Xano with a variable and a couple of loops.

    ---------------

    string temp = ''

    select fld1, fld2, fld3 from table1 as results

    for each record in results

      for each value in record[fld1]

          temp = temp + ',' + value

      next

      record.flat = temp

      temp = ''

    next

    ----------------------

    Is this the best and/or only way to do this with Xano?

    However, I want to do:

    select GROUP_CONCAT(fld1) as flat, fld2, fld3 from table1 as results

    (https://www.w3resource.com/mysql/aggregate-functions-and-grouping/aggregate-functions-and-grouping-group_concat.php)

    But, if I can't use group_concat or something like that then can I do something with a Lambda expression to get the result that I want?

  • Ray Deck
    Ray Deck Trusted Xano Expert ✭✭✭
    Answer ✓
    Options

    Most db-level functions aren't available in-query, but you can do them in the function stack. Xano supports a join filter (documented here https://docs.xano.com/working-with-data/data-type-filters/array-filter) that concatenates your array into a string without the inner loop- should be much faster.


    We discuss these kinds of data manipulation questions on our State Change Pro office hours on a daily basis. Our next is tomorrow!

Answers