[英]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.