javascript Angular - 只有在唯一时才推送到数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22719340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 23:39:35  来源:igfitidea点击:

Angular - Only push to array if unique

javascriptangularjs

提问by xXPhenom22Xx

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.

我有一个 Angular 应用程序,它收集发票项目的价值,我想确保只有唯一的项目被添加到这个集合中,但我没有运气。

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.

我正在向这个集合推送 3 条信息:id、价格和类型。我想确保当前收藏中没有与这 3 点匹配的内容。

// 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 } ] }

发票:{ 项目:[ { } , { id: 25 price: 0 type: photo } , { id: 25 price: 0 type: photo } ] }

enter image description here

在此处输入图片说明

采纳答案by xXPhenom22Xx

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';
    }
};

回答by SomeKittens

.filteris pretty much what you need.

.filter几乎是您所需要的。

$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

半人为的 JSFiddle

回答by Taran

YOu can simple pop opposite of push

你可以简单的弹出相反的推送

array.splice(array.pop(item));