transpose
transpose
(anArray: any[][]) => any[][]
Return arrays grouped by the sub-array indices.
Or as wikipedia states: it switches the row and column indices.
If this is confusing, please look at the examples. Sometimes it's easier to to show than tell.
const salesByQuarter = [ ['region', 'europe', 'america'], ['quarter 1', '52,000', '113,000'], ['quarter 2', '58,000', '127,000'], ] const salesByRegion = transpose(salesByQuarter) console.log(salesByRegion) // is [ // [region, quarter 1, quarter 2] // [europe, 52,000, 58,000] // [america, 113,000, 127,000] // ]
const salesByQuarter = [ ['region', 'europe', 'america'], ['quarter 1', '52,000', '113,000'], ['quarter 2', '58,000', '127,000'], ] const salesByRegion = transpose(salesByQuarter) console.log(salesByRegion) // is [ // [region, quarter 1, quarter 2] // [europe, 52,000, 58,000] // [america, 113,000, 127,000] // ]
const salesByQuarter = [ ['region', 'europe', 'america'], ['quarter 1', '52,000', '113,000'], ['quarter 2', '58,000', '127,000'], ] const salesByRegion = transpose(salesByQuarter) console.log(salesByRegion) // is [ // [region, quarter 1, quarter 2] // [europe, 52,000, 58,000] // [america, 113,000, 127,000] // ]
const salesByQuarter = [ ['region', 'europe', 'america'], ['quarter 1', '52,000', '113,000'], ['quarter 2', '58,000', '127,000'], ] const salesByRegion = transpose(salesByQuarter) console.log(salesByRegion) // is [ // [region, quarter 1, quarter 2] // [europe, 52,000, 58,000] // [america, 113,000, 127,000] // ]
Transposing is helpful when we work with spreadsheets. Below, we have weather data and want to print each day's forecast. Let's use transpose to group it by day for easy reporting.
const weatherByMeasurement = [ ['measurement', 'Mon', 'Tue', 'Wed'], ['high', 68, 65, 61], ['low', 55, 54, 50], ] const weatherByDay = transpose(weatherByMeasurement) const [_names, ...numbersByDay] = weatherByDay const format = ([day, high, low]) => { return [day, ` high: ${high}`, ` low: ${low}`].join('\n') } const getReport = compose([ mapValues(format), joinValues('\n'), prepend('Three Day Forecast\n'), ]) const report = getReport(numbersByDay) console.log(report) // prints // Three Day Forecast // Mon // high: 68 // low: 55 // Tue // high: 65 // low: 54 // Wed // high: 61 // low: 50
const weatherByMetric = [ ['metric', 'Mon', 'Tue', 'Wed'], ['high', 68, 65, 61], ['low', 55, 54, 50], ] type Forecast = [string, number, number] type WeatherByDay = [string[], ...Forecast[]] const weatherByDay = transpose(weatherByMetric) as WeatherByDay const [_names, ...numbersByDay] = weatherByDay const format = ([day, high, low]: Forecast) => { return [day, ` high: ${high}`, ` low: ${low}`].join('\n') } const getReport = compose([ mapValues(format)<Forecast[]>, joinValues('\n'), prepend('Three Day Forecast\n'), ]) const report = getReport(numbersByDay) console.log(report) // prints // Three Day Forecast // Mon // high: 68 // low: 55 // Tue // high: 65 // low: 54 // Wed // high: 61 // low: 50
const weatherByMeasurement = [ ['measurement', 'Mon', 'Tue', 'Wed'], ['high', 68, 65, 61], ['low', 55, 54, 50], ] const weatherByDay = transpose( weatherByMeasurement ) const [_names, ...numbersByDay] = weatherByDay const format = ([day, high, low]) => { return [ day, ` high: ${high}`, ` low: ${low}`, ].join('\n') } const getReport = compose([ mapValues(format), joinValues('\n'), prepend('Three Day Forecast\n'), ]) const report = getReport(numbersByDay) console.log(report) // prints // Three Day Forecast // Mon // high: 68 // low: 55 // Tue // high: 65 // low: 54 // Wed // high: 61 // low: 50
const weatherByMetric = [ ['metric', 'Mon', 'Tue', 'Wed'], ['high', 68, 65, 61], ['low', 55, 54, 50], ] type Forecast = [string, number, number] type WeatherByDay = [string[], ...Forecast[]] const weatherByDay = transpose( weatherByMetric ) as WeatherByDay const [_names, ...numbersByDay] = weatherByDay const format = ([day, high, low]: Forecast) => { return [ day, ` high: ${high}`, ` low: ${low}`, ].join('\n') } const getReport = compose([ mapValues(format)<Forecast[]>, joinValues('\n'), prepend('Three Day Forecast\n'), ]) const report = getReport(numbersByDay) console.log(report) // prints // Three Day Forecast // Mon // high: 68 // low: 55 // Tue // high: 65 // low: 54 // Wed // high: 61 // low: 50
Transpose requires the sub-arrays to have equal length
try { transpose([ ['a', 'b'], [1, 2, 3], ]) } catch (err) { console.log(err) // prints // transpose requires 'anArray' to contain arrays of equal length }
try { transpose([ ['a', 'b'], [1, 2, 3], ]) } catch (err) { console.log(err) // prints // transpose requires 'anArray' to contain arrays of equal length }
try { transpose([ ['a', 'b'], [1, 2, 3], ]) } catch (err) { console.log(err) // prints // transpose requires 'anArray' to contain // arrays of equal length }
try { transpose([ ['a', 'b'], [1, 2, 3], ]) } catch (err) { console.log(err) // prints // transpose requires 'anArray' to contain // arrays of equal length }