I did this Codepen to combine all the columns from one matrix in JS.
const matrix = [
['a', 'a', 'a'],
['b', 'b', 'b'],
['c', 'c', 'c']
];
const combineMatrixColumns = (matrix) => {
const biggest = matrix.reduce((acc, arr) => acc.length < arr.length ? arr : acc, []);
return biggest.reduce((acc, item, index) => {
matrix.forEach(item => item[index] ? acc = [...acc, item[index]] : null)
return acc;
}, [])
};
const result = combineMatrixColumns(matrix)
console.log(result)
For this input, the expected output as a single array with each column combined:
["a", "b", "c", "a", "b", "c", "a", "b", "c"]
My question is: You know a better approach to do it?