0

I want to display players who compose a team. So I create two objects : one for players, one for team.

    var equipes = [
            {'id_equipe' : 1 , 'id_joueur1' : 1 , 'id_joueur2' : 2 , 'id_joueur3' : 3, 'id_joueur4' : 4, 'id_joueur5' : 5, 'id_joueur6' : 6, 'id_poule' : 1, 'nbr_joueurs' : 1,'nbr_joueurs' : 6, 'nom' : "Dark vador", 'points' : 50},
            {'id_equipe' : 2 , 'id_joueur1' : 7 , 'id_joueur2' : 8 , 'id_joueur3' : 9, 'id_joueur4' : 10, 'id_joueur5' : 11, 'id_joueur6' : 12, 'id_poule' : 1, 'nbr_joueurs' : 6, 'nom' : "CEM", 'points' : 100},
            {'id_equipe' : 3 , 'id_joueur1' : 13 , 'id_joueur2' : 14 , 'id_joueur3' : 15, 'id_joueur4' : 16, 'id_joueur5' : 17, 'id_joueur6' : 18, 'id_poule' : 1, 'nbr_joueurs' : 6, 'nom' : "Test", 'points' : 25}
    ];

   // I stock with localstorage
   window.localStorage.setItem('equipes', JSON.stringify(equipes));

    var joueurs = [ 
        {'id_joueurs' : 1 , 'prenom' : "Sam" , 'nom' : "Mignot" , 'capitaine' : true},
        {'id_joueurs' : 2 , 'prenom' : "Jasmine" , 'nom' : "Test" , 'capitaine' : true},
        {'id_joueurs' : 3 , 'prenom' : "Kevin" , 'nom' : "Test2" , 'capitaine' : false},
        {'id_joueurs' : 4 , 'prenom' : "Thierry" , 'nom' : "Blabla" , 'capitaine' : false},
        {'id_joueurs' : 5 , 'prenom' : "Olivier" , 'nom' : "Blablabla" , 'capitaine' : false},
        {'id_joueurs' : 6 , 'prenom' : "Loic" , 'nom' : "Bla" , 'capitaine' : false},
        {'id_joueurs' : 8 , 'prenom' : "Sam" , 'nom' : "Mignot" , 'capitaine' : false},
        {'id_joueurs' : 9 , 'prenom' : "Sam" , 'nom' : "Mignot" , 'capitaine' : false},
        {'id_joueurs' : 10 , 'prenom' : "Sam" , 'nom' : "Mignot" , 'capitaine' : false},
        {'id_joueurs' : 11 , 'prenom' : "Sam" , 'nom' : "Mignot" , 'capitaine' : false},
        {'id_joueurs' : 12 , 'prenom' : "Sam" , 'nom' : "Mignot" , 'capitaine' : false},
        {'id_joueurs' : 13 , 'prenom' : "Sam" , 'nom' : "Mignot" , 'capitaine' : false}

    ];
    // Same thing, I stock with localstorage
    window.localStorage.setItem('joueurs', JSON.stringify(joueurs));

Now I want to know the player's name of each team (thanks to the 'id_joueur1', id_joueur2', ...). So I create this function :

function getJoueursEquipe () {

  var equipes = JSON.parse(window.localStorage.getItem("equipes")), // I recuperate the team's storage
      nbr_joueurs = equipes.nbr_joueurs, // how many player in the team, it can change
      joueurs = JSON.parse(window.localStorage.getItem("joueurs")), // I recuperate the player's storage
      joueursEquipe = null,
      id_joueur_equipe = null;

   // Here I want to recover id_joueur1, id_joueur2, id_joueur3 ... until nbr_joueurs
  for (var j = 1; j <= nbr_joueurs; j++) {
      id_joueur_equipe = "id_joueur"+j; 
      console.log(id_joueur_equipe);
      console.log(equipes.id_joueur_equipe1); //This is my problem
      // It returns "undefined 
  }

I want to recover these elements :

  • equipes.id_joueur1
  • equipes.id_joueur2
  • equipes.id_joueur3

...

I try different syntax ( like console.log(equipes.id_joueur+j) ) but without success !

I miss something ? My thinking is false ? Any clue ? Thanks in advance :)

Sam M.
  • 121
  • 2
  • 10
  • why don't you have a joueur_ids property in your equipes objects ? It would be easier I think :) But if you want it like that you made a mistake at the end : console.log(equipes[id_joueur_equipe]) – R. Foubert Jul 12 '16 at 15:23
  • Yeah, it could be easier in fact ^^ ending of the day, I don't have clear idea but with console.log(equipes[id_joueur_equipe]) ... it works perfectly ! Thanks ! – Sam M. Jul 12 '16 at 15:30
  • In fact it should not work I think... :p Because equipes is an array... So you have to loop through every items of this array and then console.log(equipes[j][id_joueur_equipe]). You made a mistake at the beginning here : nbr_joueurs = equipes.nbr_joueurs should not work. You have to loop through equipes – R. Foubert Jul 12 '16 at 15:34
  • Or maybe just add a equipeId parameter to the getJoueursEquipe function. Then build an equipe variable like that : var equipe = equipes[equipeId]. Finally you work with this variable and the getJoueursEquipe returns an array containing player ids ! But yes ^^ You should use a joueur_ids property ;) – R. Foubert Jul 12 '16 at 15:40

1 Answers1

0

I want to recover these elements :

equipes.id_joueur1 equipes.id_joueur2 equipes.id_joueur3

Hope this snippet will be useful

var equipes = [// Rest of the objects];
// get keys form the equipes object
var m = Object.keys(equipes[0]);
// Filter only the require keys
var _filteredValue = m.filter(function(item){
 return item !=="id_equipe" && item !== "id_poule" && 
 item !== "nbr_joueurs" && item !=="nom" && item !=="points"
})

// Will loop through each object of equipes
equipes.forEach(function(item){ 
 // Will loop through each filtered item
_filteredValue.forEach(function(itemFilter){
   document.write('<pre>'+itemFilter+' -- ' +_item[itemFilter]+'</pre>')
  })

})

JSFIDDLE

brk
  • 48,835
  • 10
  • 56
  • 78