1

I am using Firebase and the AngularFire library. I am looking for a way to remove all items or a range of items from a $firebaseArray object. I don't see a straightforward way to do it in the documentation. Is there some way I'm not thinking of other than looping and removing items one by one? Please tell me that's not the only way!!

2 Answers 2

1

If there isn't a method in the $firebaseArray that does what you want, you can use the array's $ref() to perform Firebase SDK-style calls. The array content will be synchronized with the changes you make through the ref.

To delete all elements, call remove on the ref itself:

function removeAll(firebaseArray) {
  return firebaseArray.$ref().remove();
}

To remove a range, perform an update in which the keys to be removed are set to null:

function removeRange(firebaseArray, start, end) {
  var keys = {};
  if (end === undefined) {
    end = firebaseArray.length;
  }
  for (var i = start; i < end; ++i) {
    keys[firebaseArray.$keyAt(i)] = null;
  }
  return firebaseArray.$ref().update(keys);
}

Both functions return promises.

Sign up to request clarification or add additional context in comments.

Comments

0

I couldn't get the firebaseArray.$ref().remove() to work as the remove() function didn't exist on the object when ordered by child, but doing the following seemed to work though:

$firebaseUtils.doRemove(firebaseArray.$ref());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.