Newbie
Newbie

Reputation: 281

How to append 2D array to 2D array with Google Apps Script?

With Google Apps Script, I like to add a 2D array to the end of an existing 2D array. But the following codes returned a 3D array with another nest inside, like “ [['AAPL', 'Strong'], ['MSFT', 'Strong'], [[ 'TSLA', 'Weak'], ['VZ', 'Neutral']]]”. I want the 2D output, like [['AAPL', 'Strong'], ['MSFT', 'Strong'], ['TSLA', 'Weak'], ['VZ', 'Neutral']]. What should be changed in the codes! Thank you for any guidance!

function test() {
  var data1 = [['AAPL', 'Strong'], ['MSFT', 'Strong']];
  var data2 = [['TSLA', 'Weak'], ['VZ', 'Neutral']];
  var output = data1;
  output.push(data2);
  console.log(output);
}

Upvotes: 1

Views: 1412

Answers (1)

idfurw
idfurw

Reputation: 5852

var output = data1.concat(data2);

Reference:

Array.prototype.concat()

Upvotes: 1

Related Questions