Skip to main content
tajinders0001
Inspiring
Oct 15, 2023 12:22 PM UTC
Solved

Merge function usage with dynamic list

Hello techies, 

I need to use the merge() function on a dynamic list but it isn't proving easy. 

Please check the following code snippet and suggest applying it on the dynamic list local!a.b

a!localVariables(
local!a: {
{ a: 1, b: { 1, 2, 3 } },
{ a: 2, b: { "asdf", 5, 6 } },
{ a: 1, b: { 7, 8 } }
},
{
merge({ 1, 2, 3 }, { "asdf", 5, 6 }, { 7, 8 }), /*working*/
merge(local!a.b[1], local!a.b[2], local!a.b[3]), /*not working*/
merge(a!forEach(local!a.b, fv!item)) /*incorrect result*/

}
)

tushark6475
Participating Frequently
Oct 16, 2023 12:32 PM UTC

In that case, this will work for you - 



a!localVariables(
  local!a: {
    { a: 1, b: { 1, 2, 3 } },
    { a: 2, b: { "asdf", 5, 6 } },
    { a: 1, b: { 7, 8 } }
  },
  a!forEach(
    items: enumerate(count(local!a.b)) + 1,
    expression: a!localVariables(
      local!index: fv!item,
      a!forEach(
        items: local!a.b,
        expression: if(
          local!index > count(fv!item),
          "",
          fv!item[local!index]
        )
      )
    )
  )
)


Note - Here I am assuming that you require the output in the "List of Variant" type. If you want output as a "list of text" or only 3 values then wrap the inner forEach() in the joinArray() with a separator ",".

12 replies

stefanhelzle0001
Brainy
Oct 15, 2023 1:42 PM UTC

What is the intended outcome? And, why do you "need" to use the merge function?

tajinders0001
Inspiring
Oct 16, 2023 3:00 AM UTC

Hi Stefan,

The intended outcome is merge({ 1, 2, 3 }, { "asdf", 5, 6 }, { 7, 8 }), somehow using local!a.b variable.

 

The use case is to build a table with columns local!a.a and dynamic rows based on combination of local!a.b

stefanhelzle0001
Brainy
Oct 16, 2023 6:23 AM UTC

I still do not have a clear idea what the final output of this operation should look like. Your input data structure is:

{
  { a: 1, b: { 1, 2, 3 } },
  { a: 2, b: { "asdf", 5, 6 } },
  { a: 1, b: { 7, 8 } }
}

Now, what is the output?

tushark6475
Participating Frequently
Oct 16, 2023 7:21 AM UTC

I guess, this is you are looking for - 

a!localVariables(
  local!a: {
    { a: 1, b: { 1, 2, 3 } },
    { a: 2, b: { "asdf", 5, 6 } },
    { a: 1, b: { 7, 8 } }
  },
  a!forEach(
    items: local!a,
    expression: fv!item.b
  )
)

tajinders0001
Inspiring
Oct 16, 2023 12:14 PM UTC

Hi Tushar,

Thank you for your response. However,

I am looking for the output {{1, "asdf", 7}, {2, 5, 8}, {3, 6, null}} and not the result of foreach

tushark6475
Participating Frequently
Oct 16, 2023 12:32 PM UTC

In that case, this will work for you - 



a!localVariables(
  local!a: {
    { a: 1, b: { 1, 2, 3 } },
    { a: 2, b: { "asdf", 5, 6 } },
    { a: 1, b: { 7, 8 } }
  },
  a!forEach(
    items: enumerate(count(local!a.b)) + 1,
    expression: a!localVariables(
      local!index: fv!item,
      a!forEach(
        items: local!a.b,
        expression: if(
          local!index > count(fv!item),
          "",
          fv!item[local!index]
        )
      )
    )
  )
)


Note - Here I am assuming that you require the output in the "List of Variant" type. If you want output as a "list of text" or only 3 values then wrap the inner forEach() in the joinArray() with a separator ",".

tushark6475
Participating Frequently
Oct 16, 2023 12:41 PM UTC