Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

242
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda