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

397
Views
Excel Office Script - error trying to push a 2D Array

I'm having an issue of understanding and I'll try to be basic with my problem because it involves lots of data. Here is a simple mockup.

From myArray below, I'm trying to create a new 2D array (arrayFinal) in the form of [[element],[element],[element]], to be able to use a setValues() with Excel Office Script. In this example, each element should have more than 2 elements.

But I'm truly not going anywhere with my way.

I only get a return like, with too many [[[]]] (one too many), and only the last element repeating itself.

[[[1,5,7]],[[1,5,7]],[[1,5,7]],[[1,5,7]],[[1,5,7]],[[1,5,7]]]

Could you have a look and tell me what's the problem ?

let myArray: number[][] = [[1, 2, 3], [2, 4], [5, 6, 7],[1],[2,3],[1,5,7]];

let testArray: (string | boolean | number)[][] = []; 
let arrayFinal: (string | boolean | number)[][] = []; 


myArray.map(x => {
    testArray.length = 0;
    if (x.length > 2) {

       
            testArray.push(x);

       
    }
    arrayFinal.push(testArray);
})
console.log(JSON.stringify(arrayFinal))

Thank you !

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

0

If I understand it correctly, you are trying to filter the original 2D array to only contain sub-arrays with more than 2 elements, right?

Wondering if you would want to try something like this:

let myArray: number[][] = [[1, 2, 3], [2, 4], [5, 6, 7],[1],[2,3],[1,5,7]];
let arrayFinal = myArray.filter(arr => arr.length > 2);
console.log(JSON.stringify(arrayFinal))

The output should be:

[[1,2,3],[5,6,7],[1,5,7]]
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!