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

251
Vistas
create a sub array from array with dynamic data

I am experimenting with multidimensional arrays and was trying to create an array with dynamic input. I get the input data by reading all the file names in a directory. Then what I did was push them to an array and iterate over them to make them a subarray. That worked but where I'm stuck at is, I was trying to check the length of the currentData array and I want to create a sub-array for every 4 elements of the currentData array. To explain this better if currentData = [file1, file2, file3, file4, file5, file6, file7] How can I manipulate the array and divide the items inside to every 4th element which result formatedArr = [[file1, file2, file3, file4], [file5, file6, file7]]. Keep in mind the length of currentData is dynamic. Any help is appreciated. Thanks in advance.

var currentData = [] //array of all file names
fs.readdirSync('./dirPATH').forEach(file => {
    currentData.push(file);
});

const m = currentData;
const n = currentData;
let formatedArr = new Array(m); // create an array of length n
for (var i = 0; i < m; i++) {
    formatedArr[i] = new Array(n); // make each element an array
}
console.log(formatedArr); //expected array of all sub array of 4th element

about 4 years ago · Juan Pablo Isaza
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