问题
One type of edges in my graph has a property called roles. It is an array/list of strings. It is like ["Bill Smoke", "Haskell Moore", "Tadeusz Kesselring", "Nurse Noakes", "Boardman Mephi", "Old Georgie"]
How can I convert this to a string? I want to join them. With JS, I can do ['asd', '1', '2'].join('')
. I want a similar functionality inside cypher
回答1:
WITH REDUCE(mergedString = "",word IN ["Bill Smoke", "Haskell Moore", "Tadeusz Kesselring", "Nurse Noakes", "Boardman Mephi", "Old Georgie"] | mergedString+word+',') as joinedString
RETURN LEFT(joinedString,SIZE(joinedString)-1)
REDUCE is the function you are basically looking for . https://neo4j.com/docs/cypher-manual/current/functions/list/#functions-reduce
回答2:
WITH ["Bill Smoke", "Haskell Moore", "Tadeusz Kesselring", "Nurse Noakes", "Boardman Mephi", "Old Georgie"] AS array,
';' AS separator
RETURN REDUCE(mergedString = "",item IN array |
mergedString
+ CASE WHEN mergedString='' THEN '' ELSE separator END
+ item) AS mergedString
回答3:
APOC Procedures has support for several categories of helper functions, this one included.
When installed, you can use apoc.text.join() to get the desired result:
WITH ["Bill Smoke", "Haskell Moore", "Tadeusz Kesselring", "Nurse Noakes", "Boardman Mephi", "Old Georgie"] as list
RETURN apoc.text.join(list, ' ') as string
来源:https://stackoverflow.com/questions/60182740/neo4j-cypher-convert-array-list-to-string