There is a specific array of 'data' of 20 lines

var data = [['classique'], ['City'], ['Radouga'], ['Hypermarche'], ['Atak'], ['U. Proximite'], ['Proximite'], ['Auchan Retail Russie'], ['Total'],['classique'], ['City'], ['Radouga'], ['Hypermarche'], ['Atak'], ['U. Proximite'], ['Proximite'], ['Specialise'], ['Ecommerce'], ['Auchan Retail Russie']]; 

And there is an array with the data 'gridData', too, out of 20 lines, I iterate over it and add it to the 'data' array. They are added as more array lines.

 var data = [['classique'], ['City'], ['Radouga'], ['Hypermarche'], ['Atak'], ['U. Proximite'], ['Proximite'], ['Auchan Retail Russie'], ['Total'],['classique'], ['City'], ['Radouga'], ['Hypermarche'], ['Atak'], ['U. Proximite'], ['Proximite'], ['Specialise'], ['Ecommerce'], ['Auchan Retail Russie']]; for(var i = 0; i < this.gridData.length; i++) { data[i].push(this.gridData[i].data); 

How to make the first line of 'gridData' added to the first line of 'data', the second line of 'gridData' to the second 'data', and so on. right now the array is done like this = data = [comparable [values ​​gridData'0 ']], [classique [values ​​gridData'1']], etc. I need to like this [comporable, values ​​gidData'0 '], [classicue, values ​​gridData'1 '] etc

  • no not like this. Give an example of input data. all 20 is not needed. Input data is the variable data , the variable is this.gridData . And sample output. That is, that in the end should be in the variable data - Grundy

1 answer 1

The push method adds elements to the end of the array. To add a value to a specific element, you need to refer to it by index:

 for(var i = 0; i < this.gridData.length; i++) { data[i].push(this.gridData[i].data); 

If an array is stored in this.gridData [i] .data , all elements of which need to be added to data [i], then you should use the spread operator

 for(var i = 0; i < this.gridData.length; i++) { data[i].push(...this.gridData[i].data); 

Or, if this statement is not yet supported, use the apply method.

 for(var i = 0; i < this.gridData.length; i++) { data[i].push.apply(data[i], this.gridData[i].data); 
  • @Gruby thank you so much for help - Evgeny Lyubimov
  • @YevgenyLyubimov, look at the updated answer, apparently this is exactly what you wanted - Grundy