1

Is there any elegant way to go from Firebase $asArray() to a regular Javascript Array? I'm looking to get the difference between the array returned from Firebase and a regular array of objects.

var purchased = $firebase(new Firebase(FIREBASE_URL + 'purchased/' + uid)).$asArray();
  purchased.$loaded(function () {
    $scope.shopping_items = $(items).not(purchased).get();
  }

The goal of the above piece of code is to get the list of items that hasn't been purchased, the list of items is just a javascript array. If I can get the purchased array to be in the same format, the code should work.

Maher Manoubi
  • 585
  • 1
  • 6
  • 18
  • possible duplicate of [An efficient way to get the difference between two arrays of objects?](http://stackoverflow.com/questions/6715641/an-efficient-way-to-get-the-difference-between-two-arrays-of-objects) – Frank van Puffelen Oct 19 '14 at 11:53

1 Answers1

3

First thing to understand about comparing objects is that two objects with identical keys and values are not equal because they are not the same object.

var x = {foo: 'bar'};
var y = {foo: 'bar'};
console.log(x === y); // false

Keep this in mind when you are trying to compare two arrays of objects.

With that said, a Firebase array is just like a regular Javascript array except for some utility methods like $add, $remove, $save.

If you wanted to get create a copy of the firebase array without the utility methods, you could use something like the following.

var regularArray = [];
for (var i = 0; i < firebaseArray.length; i++) {
  regularArray.push(firebaseArray[i]);
}

However, this doesn't doesn't really get you much closer to computing the difference of two arrays. The following might help though.

An efficient way to get the difference between two arrays of objects?

Community
  • 1
  • 1