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

249
Views
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
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!