简体   繁体   English

在我的数组中搜索某些值,然后显示有多少命中

[英]Searching my array for certain values and then displaying how many hits there are

I need some help with searching my array.我需要一些帮助来搜索我的数组。 I've created a bubble sorting filter and now I need some help with searching for certain values within it.我创建了一个冒泡排序过滤器,现在我需要一些帮助来搜索其中的某些值。

For example my array contains the numbers "1,4,5,7,8,5,5,4,3,2" and I want to see how many fives there are in the array.例如,我的数组包含数字“1,4,5,7,8,5,5,4,3,2”,我想看看数组中有多少个 5。

How do I solve this problem?我该如何解决这个问题?

Counting the occurences of a specific value is as simply as iterating over the whole array and incrementing a variable as soon as it finds that value.计算特定值的出现次数就像遍历整个数组并在找到该值后立即递增变量一样简单。

var myArray:Array = [1, 4, 5, 7, 8, 5, 5, 4, 3, 2];
var counter:int = 0;
for (var a:int = 0; a < myArray.length; a++)
{
    if (myArray[a] == 5)
    {
        counter++;
    }
}
trace("occurences of 5: " + counter);

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

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