Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

250
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda