Текст с разделителями для вложенного объекта javascript / JSON - PullRequest
0 голосов
/ 04 июня 2018

У меня есть текст в формате

var text = "{{A-B-C}{A-C-B}D-B}{E}F" //example 1

Текст может быть что-то вроде:

var text = "{{{A-B-C}{C-A-B}D-E}{B-C}E-A}{F}G" //example 2

Таким образом, структура узла может измениться, но разделение - и {} остаетсято же самое для обозначения иерархии.

Я хотел бы создать из этого дерево либо в виде объекта javascript, либо в формате JSON для макета дерева d3js.Я могу использовать lodash / jquery или любую другую библиотеку, какую захочу.

Возможный макет, который мне нужен, похож на это изображение, для приведенного выше примера текста 1 enter image description here

Как преобразовать строку во вложенные данные в форматекак ниже (для var text пример 1).Я изо всех сил пытался найти способ сделать это.Любая помощь или направление приветствуется.

var textobject = {
  name: "F",
  children: [{
      name: "E",
      children: []
    },
    {
      name: "B",
      children: [{
        name: "D",
        children: [{
          name: "B",
          children: [{
            name: "C",
            children: [{
              name: "A",
              children: []
            }, ]
          }, ]
        }, {
          name: "C",
          children: [{
            name: "B",
            children: [{
              name: "A",
              children: []
            }, ]
          }, ]
        }, ]
      }, ]
    },
  ]
}

1 Ответ

0 голосов
/ 05 июня 2018

Вы можете решить эту проблему, используя массивы в качестве стеков.Там есть правильное объяснение из комментариев кода ниже.

function parseTree(string) {
  // split string into an array
  // reduce the array into a proper tree where the 
  // first and last item will be the entire tree, hence,
  // the access of the first index from the last part
  // of this expression
  return string.split(/(}|{|\-)/)
    .reduce(parseTreeReducer, [])[0];
}

function parseTreeReducer(array, ch, index, original) {
  // always track the index of the open bracket 
  // from the array stack
  let indexBracket = array.lastIndexOf('{');

  if(ch === '{') { // is it an open bracket?
    // store it in the array stack!
    array.push(ch); 
  } else if(ch === '}') { // is it a close bracket?
    // remove the last open bracket
    // this prepares the nodes after the open bracket index
    // to be the children of the next node
    array.splice(indexBracket, 1); 
  } else if(ch !== '-' && ch) { // make sure to ignore '-' key

    // push the node in the array stack
    array.push({ 
      name: ch, // name
      // ensure that we only get the nodes that are after the 
      // last open bracket from the array stack and remove them.
      // These removed nodes will be assigned as children for
      // this current node
      children: array.splice(
        // make sure we don't delete the open bracket
        indexBracket + 1, 
        // only remove items beyond the open bracket index
        array.length - indexBracket - 1 
      ).reverse() // reverse to represent the expected output (optional!)
    });

  }
  // return the array stack
  return array;
}

function parseTree(string) {
  // split string into an array
  // reduce the array into a proper tree where the 
  // first and last item will be the entire tree, hence,
  // the access of the first index from the last part
  // of this expression
  return string.split(/(}|{|\-)/)
    .reduce(parseTreeReducer, [])[0];
}

function parseTreeReducer(array, ch, index, original) {
  // always track the index of the open bracket 
  // from the array stack
  let indexBracket = array.lastIndexOf('{');

  if(ch === '{') { // is it an open bracket?
    // store it in the array stack!
    array.push(ch); 
  } else if(ch === '}') { // is it a close bracket?
    // remove the last open bracket
    // this prepares the nodes after the open bracket index
    // to be the children of the next node
    array.splice(indexBracket, 1); 
  } else if(ch !== '-' && ch) { // make sure to ignore '-' key
  
    // push the node in the array stack
    array.push({ 
      name: ch, // name
      // ensure that we only get the nodes that are after the 
      // last open bracket from the array stack and remove them.
      // These removed nodes will be assigned as children for
      // this current node
      children: array.splice(
        // make sure we don't delete the open bracket
        indexBracket + 1, 
        // only remove items beyond the open bracket index
        array.length - indexBracket - 1 
      ).reverse() // reverse to represent the expected output (optional!)
    });

  }
  // return the array stack
  return array;
}

/* THE CODE BELOW IS ONLY FOR DEMO USAGE */

var input = document.querySelector('input');
var output = document.querySelector('pre');

setOutput();
input.addEventListener('keyup', setOutput);

function setOutput() {
  output.innerHTML = JSON.stringify(parseTree(input.value), 0, 4);
}
.as-console-wrapper{min-height:100%;top:0}
input{width: 100%}
pre{background-color: #ccc; padding: 1em}



...