Changing the index within a nested array

I’d like to move an object within the same array. I want to take the item with the index of 2 and set it to index 0. There are many usecases when doing drag&drop.

Before:

data: [
  {id: 'x', name: 'y'},  {id: 'xx', name: 'yy'},  {id: 'xxx', name: 'yyy'}
]

MAGIC N1QL query here

After:

data: [
  {id: 'xxx', name: 'yyy'}, {id: 'x', name: 'y'},  {id: 'xx', name: 'yy'}
]
 SELECT ARRAY_PREPEND(data[2],ARRAY_REMOVE(data,data[2]))
 LET data =[ {"id": 'x', "name": 'y'},  {"id": 'xx', "name": 'yy'},  {"id": 'xxx', "name": 'yyy'} ];

In Next release you can use ARRAY_MOVE()

 SELECT ARRAY_MOVE(data,2,0) 
LET data =[ {"id": 'x', "name": 'y'},  {"id": 'xx', "name": 'yy'},  {"id": 'xxx', "name": 'yyy'} ];
1 Like

I was looking for an UPDATE function, not a select

UPDATE default
SET data = ARRAY_PREPEND(data[2],ARRAY_REMOVE(data,data[2]));

This only applies if you move the value at the beginning of the array. I’m asking for a function to move 2 -> 4, 3 -> 5 etc.
Setting fromIndex and toIndex