Search code examples
neo4jcypherapoc

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


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]

Solution

  • 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]