0

i have an array with called my games it has a lot of property such as game type i want to fill some three ohter array based on the property game type but it did not work this is an exemple of the array my gam

hideScore: true, started: true, startedAt: "Fri, 02 Jul 2021 09:04:25 GMT", finished: true, finishedAt: "Fri, 20 Aug 2021 15:35:37 GMT", …}
archived: false
bgGame: "#1a1f29"
client: "client test"
company: "tunisia"
country: "Tunisia"
createdAt: "2021-07-02T09:03:34.015Z"
finished: true
finishedAt: "Fri, 20 Aug 2021 15:35:37 GMT"
gameType: "test"
hideScore: true
id: "60ded6668734e65c51f50996"
language: "English"
maxLevel1: 10.47195
maxLevel2: 2.065616666666668
maxScore: 23400
name: "game test"
nbrDoors: 8
nbrPlayersPerTeam: 8
partner: "khouloud ben khssib"
playtime: 30
started: true
startedAt: "Fri, 02 Jul 2021 09:04:25 GMT"
startsAt: "Fri, 02 Jul 2021 09:03:00 GMT"
teams: (20) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
teamsCount: 20
textColor: "#ffffff"
updatedAt: "2021-08-20T15:35:37.348Z"
user: "609321b935e3f04d04d44337"
__v: 0
_id: "60ded6668734e65c51f50996"

i have three array named as gametype

NumberofLive: any[] =[];
  NumberofTest: any[] =[];
  NumberofDemo: any[] =[];

i want every time to fill a table i wrote something like thiss

for(let i=0;i<this.MyGames.length; i++){
      
      switch(this.MyGames[i].gameType) { 
        case 'test': { 
        
          this.NumberofTest=this.NumberofTest+this.MyGames[i]           
           break; 
        } 
        case 'live': {                  

                          this.NumberofLive=this.NumberofLive+this.MyGames[i]           

           console.log('salzem');     console.log(this.NumberofLive[1].gameType)

           
           break; 
        } 
        default: {              


          this.NumberofDemo=this.NumberofDemo+this.MyGames[i]           
           break; 
        } 
     } 

    }

when i log my table i find something undefined

2 Answers 2

1

You cant just add up to array with "+" sign, you have to push new object into it.

this.NumberofTest.push(this.MyGames[i])

Also, I think a more elegant solution would be array.filter method without for loop as below:

this.NumberofTest = this.MyGames.filter((item) => item.gameType==='test')
Sign up to request clarification or add additional context in comments.

Comments

0

I have modified your code, please have a look below

for(let i=0;i<this.MyGames.length; i++){   
  switch(this.MyGames[i].gameType) { 

    case 'test':  
         this.NumberofTest.push(this.MyGames[i]);           
         break; 
         
    case 'live':                
         this.NumberofLive.push(this.MyGames[i]);   
         break; 
         
    default:             
         this.NumberofDemo.push(this.MyGames[i]);
  }  
}

Another Solution

this.NumberofTest = this.MyGames.filter((item) => item.gameType === 'test');
this.NumberofLive = this.MyGames.filter((item) => item.gameType === 'live');
this.NumberofDemo = this.MyGames.filter((item) => item.gameType !== 'test' && item.gameType !== 'live');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.