Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

210
Views
Error when trying to use push into a 2d array

I'm trying to push a value into a 2d array but I'm getting the error:

Cannot read property 'push' of undefined

Here's a MWE of my code:

var saida = new Array()
for (i in dbInfo){
  saida[i].push(i)
}

Where dbInfo is a range in my sheet, but can be replaced by any other range for this example. I need the output to be a 2d array so I can work out the values into the sheet.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

If you just created the array the array is empty. So saida[n] will be undefined no matter what value n has.

You need to first create it. Then you can push into it.

Maybe something like this? Not sure what you need.

var saida = new Array()
for (i in dbInfo){
  saida[i]=[]
  saida[i].push(i)
}
about 4 years ago · Juan Pablo Isaza Report

0

If dbInfo is an object of type Range from the Sheet, then the returned array after using getValues is already a 2D array so a simple assignment will suffice:

function addData() {
  // your other code
  let dbInfoVals = dbInfo.getValues();
  let saida = dbInfoVals;
  console.log(saida)
}

However, if dbInfo is a 1D array, transforming into a 2D one can be done by using the splice method in JavaScript:

function addData() {
  // your other code
  let saida = [];
  while (dbInfo.length){ 
    saida.push(dbInfo.splice(0, 2));
  }
  console.log(saida);
}

The second parameter of splice will give you the number of columns, so you might need to adjust this to match your range accordingly.

Reference

  • Range Class - getValues();

  • Array.prototype.splice().

about 4 years ago · Juan Pablo Isaza Report

0

You will need to define your array to 2d Eg: int[,] arr= new string[3, 2];

You will need to iterate twice.

for(int col1 = 0; col1 < arr.GetLength(0); col1++){
   for(int row1 = 0; row1 < arr.GetLength(1); row1++)
   { 
     arr[col1,row1] =  2; 
   }
 }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!