简体   繁体   English

如何在JavaScript中计算数组数组中出现的次数

[英]How can I count the number of occurrences in an array of arrays in JavaScript

How can I count the number of occurrences in an array of arrays? 如何计算数组数组中出现的次数?

So I have an array of around 280 arrays, the goals of a soccer team as such: 所以我有大约280个数组,一个足球队的目标是这样的:

var goalsScored = [["Sun Apr 28 2009","Juventus - Milan","2 - 3","Del Piero","Dida",7],["Sun Apr 28 1999","Juventus - Milan","2 - 3","Zidane","Peruzzi",44], ["Sat June 8 2009","Juventus - Milan","1 - 0","Nedved","Abbiati",7].....n=280]

So basically I have a list of goals and I want to count the number of goals scored each minute where goalsScored[5] refers to the minute. 因此,基本上,我有一个目标列表,我想计算每分钟得分的目标数量,其中goalScored [5]表示分钟。 I also want to maintain the data, I don't want to simply count how many goals were scored in the 7th minute, but I would like to know when, by who...etc. 我也想维护数据,我不想简单地计算第7分钟进球数,但是我想知道何时,由谁...等等。

So my question here is how can I do that? 所以我的问题是我该怎么做? And what would be the best data structure? 最好的数据结构是什么? Should I have an array for each minute, or an array of arrays of arrays as such: 我应该每分钟有一个数组,还是这样的数组数组:

goalsMin = [[minute, count[date ,teams, result,scorer, goalkeeper]],[minute,count[date,teams,result,scorer,goalkeeper]]]

Does that make sense? 那有意义吗?

You could keep an array of arrays (one array for each minute). 您可以保留一个数组数组(每分钟一个数组)。 The data could be mentained using Object, something like this: 可以使用Object来维护数据,如下所示:

goal = {
    date : "Sun Apr 28 2009",
    match : "Juventus - Milan",
    score : "2-3",
    player : "Del Piero",
    goalkeeper : "Dida"
};

You will insert data in the structure like this: 您将在这样的结构中插入数据:

goals[minute].push(goal);

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

相关问题 如何使用 javascript 计算字符串数组中单词的出现次数? - How can I count the occurrences of a word in an array of strings using javascript? 如何使用 JavaScript 计算 div class 的出现次数,并在没有 jQuery 的情况下将数字打印到屏幕上? - How can I use JavaScript to count the number of occurrences of a div class, and print the number to screen without jQuery? 如何计算Javascript中对象数组中不同值的出现次数? - How to count number of occurrences of distinct values from an array of objects in Javascript? 如何计算 arrays 数组中字符串的出现次数? - How to count occurrences of strings in array of arrays? 我如何计算数组中某个值的出现次数而无突变 - How do I count the number of occurrences of a value in an array without mutation 如何使用JavaScript计算嵌套数组中的数组数 - how to count number of arrays in nested array using javascript 使用Javascript计算包含特定字符串的数组中唯一出现次数 - Count the number of unique occurrences in an array that contain a specific string with Javascript 字符串中的数组出现Javascript计数 - Javascript count occurrences of Array in String 计算JavaScript数组中对象的出现次数 - Count occurrences of objects in JavaScript array 我如何计算数组中的项目数 - How can I count the number of items in array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM