lundi 22 mars 2021

How can I join one array to another multiple times in a loop?

How can I join one array to another multiple times through a loop? It is necessary to add arr to gridAreas several times and so that the result is in newGridAreas.

My script works correctly if this.state.rowsCount is 1 or 2. If this.state.rowsCount> 2, it behaves incorrectly.

If this.state.rowsCount = 2 then newGridAreas =

0: "sources auto"
1: "clicks auto"
2: "shows auto"
3: "conversion-price auto"
4: "price auto"
5: "conversion auto"
6: "sales auto"
7: "sources_twin_1 auto"
8: "clicks_twin_1 auto"
9: "shows_twin_1 auto"
10: "conversion-price_twin_1 auto"
11: "price_twin_1 auto"
12: "conversion_twin_1 auto"
13: "sales_twin_1 auto"

And if this.state.rowsCount = 3, then newGridAreas =

0: "sources auto"
1: "clicks auto"
2: "shows auto"
3: "conversion-price auto"
4: "price auto"
5: "conversion auto"
6: "sales auto"
7: "sources_twin_2 auto"
8: "clicks_twin_2 auto"
9: "shows_twin_2 auto"
10: "conversion-price_twin_2 auto"
11: "price_twin_2 auto"
12: "conversion_twin_2 auto"
13: "sales_twin_2 auto"

And I need that if this.state.rowsCount = 3, then newGridAreas =

0: "sources auto"
1: "clicks auto"
2: "shows auto"
3: "conversion-price auto"
4: "price auto"
5: "conversion auto"
6: "sales auto"
7: "sources_twin_1 auto"
8: "clicks_twin_1 auto"
9: "shows_twin_1 auto"
10: "conversion-price_twin_1 auto"
11: "price_twin_1 auto"
12: "conversion_twin_1 auto"
13: "sales_twin_1 auto"
14: "sources_twin_2 auto"
15: "clicks_twin_2 auto"
16: "shows_twin_2 auto"
17: "conversion-price_twin_2 auto"
18: "price_twin_2 auto"
19: "conversion_twin_2 auto"
20: "sales_twin_2 auto"

Here is my script:

pasteTableArticlesTwins(){
      const table = document.querySelector('.table'),
            tableArticles = document.querySelectorAll('.table__article');
      const gridAreas = ['sources auto', 'clicks auto', 'shows auto', 'conversion-price auto', 'price auto', 'conversion auto', 'sales auto'];
      
      let newGridAreas;
      
      for(let z = 1; z <= this.state.rowsCount; z++){

          const arr = gridAreas.map((i) => 
         { 
              return i.replace(/\s+/, `_twin_${z-1} `)
         
         });
          
          newGridAreas = [...gridAreas, ...arr]
          
          
          
          if(this.state.rowsCount === 1){
              newGridAreas = '';
              newGridAreas = ['sources auto', 'clicks auto', 'shows auto', 'conversion-price auto', 'price auto', 'conversion auto', 'sales auto'];
          }
          
          console.log(newGridAreas)
      }
}



Aucun commentaire:

Enregistrer un commentaire