Přidat otázku mezi oblíbenéZasílat nové odpovědi e-mailem Jaky napsat script v JS který vygeneruje takový objekt

Vytvořené narychlo, takže určitě ne ideální:

function parse(input) {
  const result = [];
  const stack = [];
  const lines = input.split("\n");
  let indent = 0;
  let col = result;
  let item;
  for (let i = 0; i < lines.length; i++) {
    const line = lines[i];
    let curInd = 0;
    while (line[curInd] === " ") curInd++;
    if (curInd > indent) {
      stack.push(col);
      col = item.children;
    }
    if (curInd < indent) {
      col = stack.pop();
    }
    indent = curInd;
    item = {
        description: line.substr(curInd),
        children: []
    };
    col.push(item);
  }
  console.log(JSON.stringify(result, null, 2));
}

Reakce na odpověď

1 Zadajte svou přezdívku:
2 Napište svou odpověď:
3 Pokud chcete dostat ban, zadejte libovolný text:

Zpět do poradny