简体   繁体   English

如何将列表(从“收集”)转换为字符串

[英]How Can I Convert a List (from Collect) Into a String

Using CYPHER I can get an ordered list of things using the collect() function. 使用CYPHER,我可以使用collect()函数获得事物的有序列表。 Is it possible to convert such a list into a simple string so that it behaves as a single string object? 是否可以将这样的列表转换为简单的字符串,使其表现为单个字符串对象?

If this isn't possible is it possible to somehow concatenate sequentially the contents of two (or more) collect statements so that in a single row I can produce output such as 'A,B,C a,b,c' where A,B,C is the ordered product of the 1st collect statement and a,b, c the second? 如果这不可能,则可以以某种方式顺序地连接两个(或多个)collect语句的内容,这样我就可以在一行中生成输出,例如“ A,B,C a,b,c”,其中A, B,C是第一个collect语句的订购产品,而a,b,c是第二个收集声明的订购产品吗?

To flesh out Dave's comment: First, you'll want to combine your collections, then use REDUCE() to append each item to a string. 充实Dave的评论:首先,您需要合并您的集合,然后使用REDUCE()将每个项目附加到字符串中。 Like so: 像这样:

WITH COLLECT(first_group) AS a, COLLECT(second_group) AS b
WITH a + b AS c
WITH REDUCE(s = HEAD(c), n IN TAIL(c) | s + ', ' + n) AS result
RETURN result

Check out the docs on REDUCE to get a better idea how it works in Cypher. 查看有关REDUCE 的文档 ,以更好地了解它在Cypher中的工作方式。

A more general solution that will also work for collections of objects (not just collections of strings) is to convert the concatenated collections into stringified JSON using the APOC procedure apoc.convert.toJson . 一个更通用的解决方案也适用于对象的集合(不仅仅是字符串的集合),是使用APOC过程apoc.convert.toJson将串联的集合转换为字符串化的JSON

This example using string collections: 此示例使用字符串集合:

WITH ['A','B','C'] AS x, ['a','b','c'] AS y, ['do','re','mi'] AS z
WITH x + y + z AS data
CALL apoc.convert.toJson(data) YIELD value
RETURN value

will return this: 将返回此:

"[\"A\",\"B\",\"C\",\"a\",\"b\",\"c\",\"do\",\"re\",\"mi\"]"

This example using object collections: 此示例使用对象集合:

WITH [{a:1},{b:2},{c:3}] AS x, [{a:10},{b:20},{c:30}] AS y, [{a:100},{b:200},{c:300}] AS z
WITH x + y + z AS data
CALL apoc.convert.toJson(data) YIELD value
RETURN value

will return this: 将返回此:

"[{\"a\":1},{\"b\":2},{\"c\":3},{\"a\":10},{\"b\":20},{\"c\":30},{\"a\":100},{\"b\":200},{\"c\":300}]"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM