Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I have an Angular application that collects values of items for an invoice, I want to make sure only unique items are being added to this collection but am having no luck.

I am pushing 3 pieces of information to this collection: id, price, and type. I want to make sure there is nothing in the collection currently matching those 3 points.

// My container
$scope.invoice = {
    items: [{
    }]
}


    $scope.addPhoto = function() {
    console.log('Withdrawing Photo: '+ $scope.item.id);
    if ($scope.invoice.items.indexOf(item.id) != $scope.item.id)
    {
      $scope.invoice.items.push({
        id: $scope.item.id,
        price: $scope.item.price,
        type: 'photo'
    });
    }
}

// Trying to avoid collections like this

invoice: { items: [ { } , { id: 25 price: 0 type: photo } , { id: 25 price: 0 type: photo } ] }

enter image description here

share|improve this question

.filter is pretty much what you need.

$scope.addPhoto = function() {
    console.log('Withdrawing Photo: '+ $scope.item.id);
    var matches = $scope.invoice.items.filter(function(datum) {
      return datum.id === $scope.item.id &&
        datum.price === $scope.item.price &&
        datum.type === $scope.item.type;
    });
    if (!matches.length)
    {
      $scope.invoice.items.push({
        id: $scope.item.id,
        price: $scope.item.price,
        type: 'photo'
    });
    }
}

Semi-contrived JSFiddle

share|improve this answer
    
Hmmmm it is still letting me add the same item twice (see above) for collection – xXPhenom22Xx Mar 28 '14 at 17:38
    
@xXPhenom22Xx There was a typo in my answer, see edit and JSFiddle. – SomeKittens Mar 28 '14 at 17:44
    
Its still letting me add the same record over and over again... :/ – xXPhenom22Xx Mar 28 '14 at 18:46
    
@xXPhenom22Xx My answer works for the use case you've put up (as demonstrated in the fiddle). I don't see how I can help further. – SomeKittens Mar 28 '14 at 18:58
    
I must be missing something because that Fiddle will not run for me, and when I use that snippet I can add the same item repeatedly – xXPhenom22Xx Mar 28 '14 at 20:26
up vote 2 down vote accepted

This is the solution I came up with to solve my problem, hopefully it helps someone else.

    $scope.addPhoto = function () {
    console.log('Withdrawing Photo: ' + $scope.item.id);
    var newItemId = $scope.item.id;
    var newItemPrice = $scope.item.price;
    var newItemType = 'photo';
    var matches = true;


    // Make sure user hasnt already added this item
    angular.forEach($scope.invoice.items, function(item) {
        if (newItemId === item.id && newItemPrice === item.price && newItemType === item.type) {
            matches = false;
            $scope.message = 'You have already selected to withdraw this item!';
        }
    });

    // add item to collection
    if (matches != false) {
        $scope.invoice.items.push({
            id: $scope.item.id,
            price: $scope.item.price,
            type: 'photo'
        });
        $scope.total += $scope.item.price;
        $scope.message = 'Total Amount Selected';
    }
};
share|improve this answer

YOu can simple pop opposite of push

array.splice(array.pop(item));
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.