Search code examples
arraysneo4jcypheroperation

Neo4j: substraction between arrays with string elements


I have the two following arrays:

 x=[red,blue,green,yellow,black,white,pink,orange,brown]
 y=[blue,white,pink]

How could I have the following output using Cypher?

 z=[red,,green,yellow,black,,,orange,brown]

I need to have an array with the same size of the array x and the elements in the same order but when there are common elements with the array y, I need to have blank elemnts.


Solution

  • You can use list comprehension.

    For example, this query:

    WITH
      ["red","blue","green","yellow","black","white","pink","orange","brown"] AS x,
      ["blue","white","pink"] AS y
    RETURN [i IN x | CASE WHEN i IN y THEN "" ELSE i END] AS result
    

    returns:

    ╒══════════════════════════════════════════════════════════╕
    │"result"                                                  │
    ╞══════════════════════════════════════════════════════════╡
    │["red","","green","yellow","black","","","orange","brown"]│
    └──────────────────────────────────────────────────────────┘