1. /**
  2. * Compacts an array of arrays into a single array.
  3. * Useful for quick prototyping and reducing output verbosity.
  4. * @param {Array<Array<any>>} arr2d The 2D array to compact.
  5. * @returns {Array<any>} A flattened array.
  6. */
  7. function compactArrays(arr2d) {
  8. // Use reduce to flatten the array.
  9. return arr2d.reduce((acc, curr) => acc.concat(curr), []);
  10. }
  11. /**
  12. * Compacts an array of arrays of numbers into a single array of numbers.
  13. * @param {Array<Array<number>>} arr2d The 2D array to compact.
  14. * @returns {Array<number>} A flattened array of numbers.
  15. */
  16. function compactArraysNumbers(arr2d) {
  17. return arr2d.reduce((acc, curr) => acc.concat(curr), []);
  18. }
  19. /**
  20. * Compacts an array of arrays of strings into a single array of strings.
  21. * @param {Array<Array<string>>} arr2d The 2D array to compact.
  22. * @returns {Array<string>} A flattened array of strings.
  23. */
  24. function compactArraysStrings(arr2d) {
  25. return arr2d.reduce((acc, curr) => acc.concat(curr), []);
  26. }

Add your comment