У меня есть многострочный комментарий, в котором определенные строки имеют метки.
Например:
[
'Label1: this is the first line',
'Label2: this is the second line',
'this is the third line',
'this is the fourth line',
'Label3: this is the fifth line' ]
Я бы хотел сжать этот массив так, чтобы он распознавал, когда строка не имеет метки, к ней присоединяется последняя строка, которая имеет метку.
Желаемый результат:
[
'Label1: this is the first line',
'Label2: this is the second line \n this is the third line \n this is the fourth line',
'Label3: this is the fifth line' ]
Я пытаюсь выполнить двойной цикл, но он идентифицирует строки, которые просто не помечены текущим индексом.
else if (!isLineLabeled(lines[j+1], labels[i])){
}
function isLineLabeled(line, label) {
return line.trim().toLowerCase().startsWith(label.toLowerCase());
}
function combineLines(lines) {
let arr = [];
const labels = ['Label1', 'Label2', 'Label3'];
for (let i = 0; i < labels.length; i++) {
for (let j = 0; j < lines.length; j++) {
if (isLineLabeled(lines[j], labels[i])) {
linesObj.push(lines[j]);
}
}
}
return arr;
}