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