-
Notifications
You must be signed in to change notification settings - Fork 7.1k
/
CountAllMatching.js
46 lines (39 loc) · 1.37 KB
/
CountAllMatching.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2020 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var SafeRange = require('./SafeRange');
/**
* Returns the total number of elements in the array which have a property matching the given value.
*
* @function Phaser.Utils.Array.CountAllMatching
* @since 3.4.0
*
* @param {array} array - The array to search.
* @param {string} property - The property to test on each array element.
* @param {*} value - The value to test the property against. Must pass a strict (`===`) comparison check.
* @param {number} [startIndex] - An optional start index to search from.
* @param {number} [endIndex] - An optional end index to search to.
*
* @return {number} The total number of elements with properties matching the given value.
*/
var CountAllMatching = function (array, property, value, startIndex, endIndex)
{
if (startIndex === undefined) { startIndex = 0; }
if (endIndex === undefined) { endIndex = array.length; }
var total = 0;
if (SafeRange(array, startIndex, endIndex))
{
for (var i = startIndex; i < endIndex; i++)
{
var child = array[i];
if (child[property] === value)
{
total++;
}
}
}
return total;
};
module.exports = CountAllMatching;