# 如何在控制台的一行中将两个多行字符串放在一起？

##### 问题描述投票：0回答：5

``````let time0 =`
a8888a
d8' ..8b
88 .P 88
88 d' 88
Y8'' .8P
Y8888P`
let time1 =`
d88
88
88
88
88
d88P`
``````

``````1
2
:
...
``````
javascript
##### 5个回答
0

`````` function mergeDigits(...digits) {
const result = [];

for(const digit of digits) {
for(const [index, row] of digit.split("\n").entries())
result[index] = (result[index] || "") + row + " ".repeat(10 - row.length);
}

return result.join("\n");
}

console.log(mergeDigits(time0, time1));
``````

0

``\n``

0

``````let time0 =`
a8888a
d8' ..8b
88 .P 88
88 d' 88
Y8'' .8P
Y8888P  `.split('\n')
let time1 =`
d88
88
88
88
88
d88P`.split('\n')

for (let i = 1; i < time0.length; i++) {
console.log(time0[i] + '  ' + time1[i]);
}``````

0

``````    let time0 =`
a8888a
d8' ..8b
88 .P 88
88 d' 88
Y8'' .8P
Y8888P  `;
let time1 =`
d88
88
88
88
88
d88P`;

function concat(number1, number2) {
var parts1 = number1.split('\n');
var parts2 = number2.split('\n');
var result = [];
parts1.forEach((line, index)=>{
var line2 = parts2[index] || '';
result.push(line + ' ' + line2);
});
return result.join('\n');
}
var result = concat(time0, time1);
console.log(result)``````

0

``````function joinPatterns(...args) {
const patterns = args.map(pat => pat.split(/[\n\r]+/));
const widths = patterns.map(lines => Math.max(...lines.map(line => line.length)));
const length = Math.max(...patterns.map(lines => lines.length));
return Array.from({length}, (_, i) =>
patterns.map((lines, j) => (lines[i]||"").padEnd(widths[j], " ")).join(" ")
).join("\n");
}

let time0 =`
a8888a
d8' ..8b
88 .P 88
88 d' 88
Y8'' .8P
Y8888P`
let time1 =`
d88
88
88
88
88
d88P`

const res = joinPatterns(time0, time1, time0);

console.log(res);``````

``````const formatChars = (chars, spacing) => {
const cs = Object.entries(chars)
.map(([k, c]) => [k, c.split('\n')])
.reduce((a, [k, c]) => ({...a, [k]: c}), {})
return (str) =>
('' + str).split('').map(c => cs[c])
.reduce((c1, c2) => {
const width = Math.max(...c1.map(c => c.length)) + spacing
return c1.map((c, i) => c.padEnd(width, ' ') + c2[i])
}).join('\n')
}

const chars = {
'0': `
a8888a
d8' ..8b
88 .P 88
88 d' 88
Y8'' .8P
Y8888P  `,
'1': `
d88
88
88
88
88
d88P`
}

console.log(formatChars(chars, 1)('11010'))
console.log(formatChars(chars, 4)('11010'))``````