Format of Lists on RETURN - [] ""

Hi,

Can anyone suggest a better way to format the RETURN list?

MATCH (a)-[:CONTAINS_INGREDIENT]->(i:ingredient)
RETURN
REDUCE(h = '', ingredient in collect(i.ingredientName) | h+ ', ' + ingredient) AS ingredientList

This yields a response in the form:
", Eggs, Butter, Milk"

Not sure how to get rid of first comma.

Many thanks :)

Hi @subs

This is one of the solutions.

MATCH (a)-[:CONTAINS_INGREDIENT]->(i:ingredient)
RETURN apoc.text.join(collect(i.ingredientName), ',') AS ingredientList
1 Like

You can try something like this if you want to stay with cypher:

with ['a', 'b', 'c'] as x
with reduce(s='',i in x | s + ',' + i) as y
return right(y,size(y)-1)
1 Like

Many thanks Gary. This works, but it returns the items as separate records. I was trying to avoid this so it can be exported else where as single string list. Is there a solution which would return all items in the one record?

Thanks Koji. Works a treat.

I did notice one strange quirk. When using RETURN apoc.text once, it was fine, but when using it twice eg RETURN apoc.tex...AS ingredientList, apoc.text.....AS stepsList
then it would return the correct items in the list but they would be repeated three times.
When using it for RETURN x3 lists, then it returned the items in the list six times.

Is there any way to avoid this?

MATCH (a)-[:CONTAINS_INGREDIENT]->(i:ingredient)
with reduce(s='',i in collect(i.ingredientName) | s + ',' + i) as singleString
return right(singleString,size(singleString)-1) AS ingredientList