how to increase/decrease the value on an element in a neo4j cypher list

81 Views Asked by At

I have a cypher list say slots=[1,2,3,4]. How can I do something like this: slots[0] +=1 so that slot[0] would now be increased by 1. I couldn't find a SET command or an APOC that would allow this.

This is my expected result:

[2, 2, 3, 4]
2

There are 2 best solutions below

6
jose_bacoy On BEST ANSWER

A simple list comprehension will do. Using index from 0 to 3, add one to the item if it is first element (index 0), else copy the value of the item in the list

WITH [1,2,3,4] as slots
RETURN [ i IN range(0, size(slots)-1) |
    case when i=0 then slots[i]+1 else slots[i] end ]

Result:

[2, 2, 3, 4]
0
Graphileon On

You can use REDUCE() for this

WITH [1,2,3,4] as myList
RETURN REDUCE(list=[], i IN myList | list + [i + 1])