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*/
})
Discussion posts and replies are publicly visible
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 ) )
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
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] ) ) ) ) )
Yeah this looks like a good outcome! For what it's worth, since a!forEach() came out, I have entirely stopped using the merge() function. You can generally achieve anything you needed to from merge() with a!forEach(), and a!forEach() is much easier to debug or set up complex logic.
A small optimization
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)), expression: a!localVariables( local!index: fv!index, a!forEach( items: local!a.b, expression: index(fv!item, local!index, "") ) ) ) )
I guess, there is always some space for optimization.