neo4j cypher convert array/list to string

浪尽此生 提交于 2020-12-12 06:57:27

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!