Trying to concat multiple strings

Hii, I am trying to concat multiple strings

a!localVariables(
local!data:{
a!map(id:1,visitorId:11,name:"John",comment:"hii john"),
a!map(id:2,visitorId:12,name:"Jacob",comment:"hii jacob"),
a!map(id:3,visitorId:13,name:"David",comment:"hii david"),
a!map(id:2,visitorId:14,name:"Bobby",comment:"hii bobby"),
a!map(id:3,visitorId:15,name:"Mark",comment:"hii mark"),
},
local!distinctIds:{1,2,3},
a!forEach(
local!distinctIds,
{
Id:fv!item,
comment:concat(index(local!data,where(index(local!data,"id",null)=fv!item),"name",null)," - ",index(local!data,where(index(local!data,"id",null)=fv!item),"comment",null))
}
)
)

Above code gives me the output as:

Id: 1

    comment: "John - hii john"

      Id: 2

      comment: "JacobBobby - hii jacobhii bobby"

      Id: 3

      comment: "DavidMark - hii davidhii mark"

      But I want output as:

      Id: 2

      comment: "Jacob - hii jacob"

                      "Bobby - hii bobby"

      Id: 3

      comment: "David - hii david"

                       "Mark - hii mark"

      Can anyone fix this?

        Discussion posts and replies are publicly visible

      • 0
        Certified Lead Developer

        Try the following

        a!localVariables(
          local!data:{
            a!map(id:1,visitorId:11,name:"John",comment:"hii john"),
            a!map(id:2,visitorId:12,name:"Jacob",comment:"hii jacob"),
            a!map(id:3,visitorId:13,name:"David",comment:"hii david"),
            a!map(id:2,visitorId:14,name:"Bobby",comment:"hii bobby"),
            a!map(id:3,visitorId:15,name:"Mark",comment:"hii mark"),
          },
          local!distinctIds: union(
            property(local!data, "id", null),
            property(local!data, "id", null)
          ),
          a!flatten(
            a!forEach(
              items: local!distinctIds,
              expression: {
                a!localVariables(
                  local!tempData: index(
                    local!data,
                    wherecontains(
                      tointeger(fv!item),
                      tointeger(property(local!data, "id", null))
                    ),
                    null
                  ),
                  a!map(
                    id: fv!item,
                    comment: a!forEach(
                      items: local!tempData,
                      expression: concat(
                        property(fv!item, "name", null),
                        " - ",
                        property(fv!item, "comment", null)
                      )
                    )
                  )
                )
              }
            )
          )
        )

        Output:

        You can use joinarray to combine more than one comment.

      • 0
        Certified Lead Developer

        I tried to simplify that code a bit and added a line break "char(10)" between the comments.

        a!localVariables(
          local!data:{
            a!map(id:1,visitorId:11,name:"John",comment:"hii john"),
            a!map(id:2,visitorId:12,name:"Jacob",comment:"hii jacob"),
            a!map(id:3,visitorId:13,name:"David",comment:"hii david"),
            a!map(id:2,visitorId:14,name:"Bobby",comment:"hii bobby"),
            a!map(id:3,visitorId:15,name:"Mark",comment:"hii mark"),
          },
          local!distinctIds: union(local!data.id, local!data.id),
          a!forEach(
            items: local!distinctIds,
            expression: a!localVariables(
              /* Find all rows with that given id */
              local!matchingRows: index(local!data, wherecontains(fv!item, local!data.id), {}),
              a!map(
                id: fv!item,
                comment: joinarray(
                  a!forEach(
                    items: local!matchingRows,
                    expression: concat(fv!item.name, " - ", fv!item.comment)
                  ),
                  char(10)
                )
              )
            )
          )
        )