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

243
Views
How to get numbers out of a 2D array to a 1D array

I have an argument that passes me a 2D array and I want to get the numbers from that array and put them on a normal array.

The argument passed to me = ([1, 3, 2], [5, 2, 1, 4], [2, 1]) What I want to do = [1, 3, 2, 5, 2, 1, 4, 2, 1]

Thanks in advance, I think I'm having a brain fart over here!

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

0

You can use the flat method of Array!

const arr = [[1, 2, 3], [4, 5, 6]]

console.log(arr.flat()) 
// [1, 2, 3, 4, 5, 6]

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat

about 4 years ago · Juan Pablo Isaza Report

0

Iterate the array and add all data in new array with concat() method.

var arr = [[1, 3, 2], [5, 2, 1, 4], [2, 1]];
var newArr = [];


for(var i = 0; i < arr.length; i++)
{
    newArr = newArr.concat(arr[i]);
}

console.log(newArr);

about 4 years ago · Juan Pablo Isaza Report

0

You can use array flat method to convert 2d array to 1d array

const arr = [
      [1,2,3],
      [4,5,6]
    ].flat(1); //flat(depth:Number)

console.log(arr);

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