O
O
Oleg2015-09-04 21:19:22
JavaScript
Oleg, 2015-09-04 21:19:22

How to organize the relationship between objects?

I have an array containing objects representing sports teams. Matches are held between the teams "each with each". How to implement the storage of statistics of matches played in the properties of an object (do not offer a DBMS) with the ability to call the result of confrontations? Perhaps there are already existing solutions to such problems?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
AtomKrieg, 2015-09-04
@ptrvch

//каждая команда включает массив номеров матчей. номер матча - индекс в массиве all_matches
var teamN = {name: "Somename", matches: [match1, match2...]};
// каждый матч это структура данных по матчу, 2 команды и допинфо, например счет
var matchN = {team1: N, team2: K, score1 : n, score2: k};

//храним все данные в таком виде.
var all_teams = [team1, team2...];
var all_matches = [match1, match2,...]

/*выбираем все для teamN*/
function get_statistics(N, K)
{
  var teamN = all_teams[N];
  for (var i = 0; i<teamN.matches.length; i++)
  {
    var m = all_matches[teamN.matches[i]];

    //Если нужно только противостояние teamN vs teamK, это такой if
    if (m.team1 == K || m.team2 == K)
    {
      //обработка результата противостояния
    }
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question