Rami Taibah
Rami Taibah

Reputation: 193

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:

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. 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.

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?

Upvotes: 0

Views: 396

Answers (1)

gabitzish
gabitzish

Reputation: 9691

You could keep an array of arrays (one array for each minute). The data could be mentained using Object, something like this:

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);

Upvotes: 1

Related Questions