简体   繁体   English

检查项目在数组中出现的次数

[英]Checking the number of times an item occurs in an array

This is my code: 这是我的代码:

<html> 
    <head>
            <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 
            <link href="app.css" rel="stylesheet" type="text/css"/>
            <title>TNF FANTASY</title>
    </head>
    <body>
        <div class="app" ng-app="myApp" ng-controller="myCtrl">
            <div class="total">
                <i><h4>Money Remaining: <br>{{total |currency}}</h4></i> <h4>{{playersSelected}}</h4>
            </div>

            <ul>  
                <li class="players" ng-repeat="player in players">
                    {{player.name}} <br> 
                    {{player.Team}} <br> 
                    {{player.price| currency}} <br>
                    {{player.position| uppercase }}
                    <br>
                    <button ng-click="buy($index)">buy</button>
                </li>
                </ul>
        </div>

        <script>
            var app = angular.module('myApp', []);
            app.controller('myCtrl', function($scope) {
            $scope.playersSelected = "0/6";
            $scope.total = 50000000;

            // keep track of what was bought already
            $scope.history = [];
            $scope.denteries = [];
            $scope.players = [
              {name : "Yasin 'YB' Amusan", Team : "Industry", price: 8000000, position: 'forward', image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
              {name : "Hassan 'Hasi' Akinyera", Team : "Industry", price: 5000000, position: 'defender', image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
              {name : "Femi 'Fabio' Awoniyi", Team : "Industry", price: 9000000, position: 'defender',image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
              {name : "Deji 'Dej' Awoniyi", Team : "Industry", price: 7000000, position: 'forward',image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
              {name : "Koye 'K10' Kekere-Ekun", Team : "Industry", price: 9000000, position:'midfielder', image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
              {name : "Teni 'Teezee' Zacchaeus", Team : "Industry", price: 6000000, position:'hybrid', image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
              {name : "Bolaji 'Boj' Odojukan", Team : "Industry", price: 7000000, position:'forward', image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
              {name : "Ernest", Team : "Industry", price: 6000000, position:'defender',image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png' },
              {name : "Fikayo 'Kyo' Etti", Team : "Industry", price: 8000000, position:'Defender',image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
              {name : "Efe Tunde-Imoyo", Team : "Industry", price: 5000000, position:'defender', image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
              {name : "Temi 'Forbes' Afolabi", Team : "VGC", price: 14000000, position:'forward', image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
              {name : "Dami Etomi", Team : "VGC", price: 7000000, position:'defender', image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
              {name : "Tomi 'Belg' Belgore", Team : "VGC", price: 8000000, position:'defender',image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
              {name : "Tobi 'Staxx' Kasali", Team : "VGC", price: 9000000, position:'Hybrid', image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
              {name : "Bobo", Team : "VGC", price: 9000000, position:'Hybrid', image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
              {name : "Ola Adeyemi", Team : "VGC", price: 5000000, position:'defender', image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
              {name : "Pekun 'Pyzzle' Odutola", Team : "VGC", price: 9000000, position:'Hybrid', image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
              {name : "Oreva 'Revz' Amata", Team : "VGC", price: 11000000, position:'forward', image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
              {name : "Tenny 'TK' Karim", Team : "YMFC", price: 5000000, position:'Forward', image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
              {name : "Andy Inegbese", Team : "YMFC", price: 5000000, position:'Midfielder', image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
              {name : "Donald 'DO' Ofik", Team : "YMFC", price: 7000000, position:'defender', image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
              {name : "Zachy Mbadiwe", Team : "YMFC", price: 10000000, position:'Forward', image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
              {name : "Emeke 'Mexxo'", Team : "YMFC", price: 6000000, position:'midfielder', image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
              {name : "Ife 'I-Baxx Bakare", Team : "YMFC", price: 6000000, position:'midfielder', image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
              {name : "Gabriel 'Gabby' Inegbese", Team : "YMFC", price: 8000000, position:'Hybrid',image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
              {name : "Jimi Oyelola", Team : "YMFC", price: 5000000, position:'Forward', image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
              {name : "Chad 'Chadea' Oyefolu", Team : "ChadFC", price: 5000000, position:'Forward',image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
              {name : "Ekoh 'Starboy' Sagoe", Team : "ChadFC", price: 15000000, position:'midfielder',image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
              {name : "Tomiwa 'JBET' Jaiyeola", Team : "ChadFC", price: 13000000, position:'Forward',image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
              {name : "Lemar Chris", Team : "ChadFC", price: 9000000, position:'defender',image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
              {name : "Olumide 'Olic' Williams", Team : "ChadFC", price: 7000000, position:'defender',image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
              {name : "Leke 'LVD' Dokomu", Team : "ChadFC", price: 7000000, position:'midfielder',image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
              {name : "Dola Awosika", Team : "ChadFC", price: 5000000, position:'defender',image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
              {name : "Nehemiah 'Memphis'", Team : "ChadFC", price: 7000000, position:'midfielder',image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
              {name : "Kola 'Skippo' Ayanwale", Team : "ChadFC", price: 10000000, position:'midfielder',image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
              {name : "Milekan 'Mileks'", Team : "ChadFC", price: 7000000, position:'defender',image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
              {name : "Lanre 'Lavigz' Vigo", Team : "ChadFC", price: 8000000, position:'hybrid', image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
              {name : "Alfred 'Flyz' Obande", Team : "FBGFC", price: 6000000, position:'defender', image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
              {name : "Chuka 'Zingy' Azinge", Team : "FBGFC", price: 6000000, position:'hybrid',image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
              {name : "Osobase 'Oso' OmoKhodion", Team : "FBGFC", price: 10000000, position:'midfielder',image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
              {name : "Arnold Okuguni", Team : "FBGFC", price: 7000000, position:'defender',image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
              {name : "Ikenna Mbadiwe", Team : "FBGFC", price: 6000000, position:'defender',image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
              {name : "Moukhtar", Team : "FBGFC", price: 8000000, position:'forward',image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
              {name : "Chinedu 'Anuxx' Anukwem", Team : "FBGFC", price: 8000000, position:'forward',image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
              {name : "Tosan 'Mac' Wiltshere", Team : "FBGFC", price: 6000000, position:'defender',image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
              {name : "Martin Agbaso", Team : "FBGFC", price: 5000000, position:'forward',image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'} 
            ];

             $scope.buy = function(i) {
                var p = $scope.history.indexOf(i);

                if (p !== -1) { //if bought, cancel it
                    $scope.history.splice(p, 1);
                    $scope.total += $scope.players[i].price;
                    $scope.playersSelected = $scope.history.length + "/6";
                } else { //else add it to history
                    if ($scope.total < $scope.players[i].price) {
                        return;
                    }
                    $scope.history.push(i);
                    $scope.total -= $scope.players[i].price;
                    $scope.playersSelected = $scope.history.length + "/6";
                }
            };
          });
        </script>
    </body>
</html>

I am trying to make each user only able to buy two players with the position "defender" or "hybrid", two players with the position "midfielder" or "hybrid", and two players with the position "forward" or "hybrid". 我试图使每个用户只能购买两名位置为“后卫”或“混合型”的球员,两名位置为“中场”或“混合型”的球员以及两名位置为“前进”或“混合型”的球员。 This makes 6 players available to buy in total. 这样一来,共有6个玩家可以购买。

I have tried using indexOf but this would only reveal the positioning of selected players in the history array, and not the number of times a player with each position above is bought. 我尝试使用indexOf,但这只会显示所选球员在历史记录数组中的位置,而不是显示每个位置以上的球员被购买的次数。

Is this something that can be done with Angular JS or is it too ambitious? 这是Angular JS可以完成的事情还是太雄心勃勃?

You can write a helper function for that purpose: 您可以为此编写一个辅助函数:

function getOccurrences(array, item) {
    var occurrences = 0;
    for (var index in array) {
        if (array[index] === item) {
            occurrences++;
        }
    }
    return occurrences;
}

EDIT: 编辑:

Based on the things written in the comment section, I realized the code I have provided is not enough. 基于注释部分中编写的内容,我意识到仅提供的代码是不够的。 So, let's change the function to this: 因此,让我们将函数更改为:

function getOccurrences(players, array, item, helper) {
    var occurrences = 0;
    for (var index in array) {
        if (((typeof helper === "function") && (helper(players, array[index], item))) || ((typeof helper !== "function") && (array[index] === item))) {
            occurrences++;
        }
    }
    return occurrences;
}

We need a helper for this purpose, like this: 为此,我们需要一个帮助者,例如:

function myHelper(players, val, item) {
    return players[val] && (players[val].position === item);
}

and then call it like this: 然后这样称呼它:

getOccurrences($scope.players, $scope.history, "defender", myHelper);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM