How to group multiple sets of duplicate integers in an array into their own array of arrays?

I am trying to split an array of integers into an array of arrays by duplicate values. The original array is composed of a list of 6 digit integers, some of these integers come in pairs, others come in groups of 3 or 4s. I'd like to get these duplicates pushed to their own arrays and have all of these arrays of duplicates composed into an array of arrays that I can later loop through.

I've looked on in the lodash library for some method or combination of but can't quite find anything that seems to work. I've also tried a few different configurations with nested for loops but also am struggling with that.

const directory = "X/";
let files = fs.readdirSync(directory);
let first6Array = [ ];
for(i=0; i< files.length; i++){
 let first6 = files[i].substring(0, 6);
 first6Array.push(first6);
};
console.log(first6Array);

example output of first6Array: [ '141848', '141848', '141848', '142851', '142851', '143275', '143275']

I'd like to end up with something like

let MasterArray = [[141848,141848,141848],[142851,142851],[143275,143275]];

#javascript #node-js #arrays

4 Likes1.65 GEEK