Home > Back-end >  How to push this array into an 2D array using GAS?
How to push this array into an 2D array using GAS?

Time:02-05

How can I have ar2 pushed into ar1 so as to have them like the Expected Output below?

ar1 = 
[
[true,160,"Blusa Crepe Barcelona manga curta",2,"","","",100],
[true,161,"Blusa Crepe Barcelona manga curta",1,"","","",100]
]
ar2 = ["Concluído",1000]

Expected Output

result = [
[true,160,"Blusa Crepe Barcelona manga curta",2,"","","",100,"Concluído",1000],
[true,161,"Blusa Crepe Barcelona manga curta",1,"","","",100,"Concluído",1000]
]

I've tried doing it using a douple for loop, but I haven't been able to make it right, as it pushes ar2 as another array in the outer array:

let output= [];
for (let i = 0; i < ar1.length; i  ){
    for (let j = 0; j < ar1[i].length; j  ){
        output.push(ar1[j],ar2)
    }
}

Appreaciate your help.

CodePudding user response:

To concat the array you can use concat() method

ar1 = 
[
[true,160,"Blusa Crepe Barcelona manga curta",2,"","","",100],
[true,161,"Blusa Crepe Barcelona manga curta",1,"","","",100]
]
ar2 = ["Concluído",1000]

for(let i = 0; i<ar1.length;i  ){
 ar1[i] = ar1[i].concat(ar2)
}

console.log(ar1)

For more info on concat() please refer here

  •  Tags:  
  • Related