I feel like I'm so close, but I can't figure this out.
I have a data tree in Firebase with events and under each event is a list of participants with their user id and name.
-events
-UniqueID
-participants
-userID
facebook_id: "12345678"
display_name: "John Doe"
-userID
facebook_id: "12345678"
display_name: "John Doe"
-userID
facebook_id: "12345678"
display_name: "John Doe"
In my routes I have a resolve function that queries the Facebook API for users who have my app installed and passes a friends
object into the controller.
This is a page for the user to invite friends to the event. I want to check the participants array and remove users who are already in the event from the friends
object.
I loop through the $firebaseArray
and use lodash to find the corresponding id. Lodash's _.find returns the object if found or undefined
if not found.
I found this removeItemsById
function in another answer here, but it doesn't seem to work.
The _.find
works correctly because I see it log the matched item, but it doesn't get removed from the object. Any ideas?
$scope.participants = $firebaseArray(firebase.database().ref().child("events").child($stateParams.eventID).child("participants"));
$scope.participants.$loaded().then(function(){
console.log(friends);
angular.forEach($scope.participants, function(part){
var i = _.toString(part.facebook_id);
console.log(part.facebook_id);
var f = _.find(friends, {'id': i});
console.log("f value is: " + f);
if (f) {
console.log("Found matching facebook id: " + part.facebook_id);
removeItemsById(friends, part.facebook_id);
}
})
$scope.friends = friends;
})
function removeItemsById(arr, id) {
var i = arr.length;
if (i) { // (not 0)
while (--i) {
var cur = arr[i];
if (cur.id == id) {
arr.splice(i, 1);
}
}
}
}