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.

1

1 Answer 1

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?

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

2 Comments

The link to the other question was very helpful and helped me solve the problem. Thank you.
Thanks for accepting the answer. Any chance I could get an upvote too? It would push me over the 50 point threshold. =)

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.