Solution 1 :

There is no way to remove an item at an index and then update all subsequent indexes in one operation. You will have to:

  1. Read the entire parent node into your application code.
  2. Remove the item from the array in your application code.
  3. Write the entire array back to the database.

And if other users may be manipulating the array at the same time, you’ll have to do this using a transaction to prevent the updates from conflicting with each other.

To find out more about a better way to deal with lists of data in Firebase, I recommend reading the documentation on appending items to a list of data and the blog post Best Practices: Arrays in Firebase.

Solution 2 :

I did some digging, I cant do it the way I wanted. I realized it thanks to Frank’s answer.
I did it this way to find index of each item:

  ref.on('value',function(snap)
{
  snap.forEach(function(child)
{
  console.log(child.ref_.path.pieces_[1]); // This will return mentioned [index] from database

});
});

Problem :

So I have this code:

firebase.database().ref('putninalog/'+ NadiiIndeksNaloga()).remove();

This removes etc. [0] and all data connected to that index in database (it doesn’t matter which number exactly).
When I remove that index how do I update all other indexes to start from 0.

Example:

[0] [1] [2] [3]

I remove: [0]

This stays:

[1] [2] [3]

I want this:

[0] [1] [2]

Comments

Comment posted by nniks19

thank you for your answer! I was trying to make my code more simple. But there is no way to do it then. I wanted to use index of array and compare it to the index in database.

Comment posted by Frank van Puffelen

The simplest way to not have to deal with this is to not use sequential numerical/array indices, but use Firebase

By