JavaScript按路径递归查找

时间:2019-03-07 14:18:24

标签: javascript recursion ecmascript-6

我具有以下数据结构:

public static void CombineMultiplePDFs(string[] fileNames, string outFile)
        {
            var lang = "en";
            var title = "My new title";

            // step 1: creation of a document-object
            Document document = new Document();

            // step 2: we create a writer that listens to the document
            FileStream newFileStream = new FileStream(outFile, FileMode.Create);
            PdfCopy writer = new PdfCopy(document, newFileStream);

            writer.SetTagged();

            writer.PdfVersion = PdfWriter.VERSION_1_7;
            writer.AddViewerPreference(PdfName.DISPLAYDOCTITLE, new PdfBoolean(true));
            writer.Info.Put(PdfName.TITLE, new PdfString(title));
            writer.CreateXmpMetadata();

            // step 3: we open the document
            document.Open();

            // set meta data
            document.AddLanguage(lang);
            document.AddTitle(title);

            // keep an array of all open readers so they can be closed again.
            var readers = new PdfReader[fileNames.Length];
            for (var fi = 0; fi < fileNames.Length; fi++)
            {
                // we create a reader for a certain document
                var fileName = fileNames[0];
                PdfReader reader = new PdfReader(fileName);
                readers[fi] = reader;
                reader.ConsolidateNamedDestinations();

                // step 4: we add content
                for (int i = 1; i <= reader.NumberOfPages; i++)
                {
                    // IMPORTANT: the third param is is "KeepTaggedPdfStructure"
                    PdfImportedPage page = writer.GetImportedPage(reader, i, true);
                    writer.AddPage(page);
                }
            }

            // step 5: we close the document and writer
            writer.Close();
            document.Close();

            // close readers only after document is lcosed
            foreach (var r in readers)
            {
                r.Close();
            }
        }

我需要一个函数来获取路径给定的最新对象。例如。 [ { name: 'root', children: [ { name: 'page', children: [ // and so on ] } ] } ] 应返回

getCurrentTree('root.page')

希望您能理解我的意思!我知道我应该递归地做,但是递归一直令我头疼。另外,我不确定是否应该对 { name: 'page', children: [ // and so on ] } find进行此操作?甚至filter?有人有聪明的主意吗?

欢呼

2 个答案:

答案 0 :(得分:2)

您可以检查名称并为子代进行迭代或返回对象。

<mat-button-toggle-group name="fontStyle" aria-label="Font Style">
<mat-button-toggle value="bold">Bold</mat-button-toggle>
<mat-button-toggle value="italic">Italic</mat-button-toggle>
</mat-button-toggle-group>

答案 1 :(得分:1)

类似吗?

let mainList = [
  {
    name: 'root',
    children: [
      {
        name: 'page',
        children: [

        ]
      }
    ]
  }
]

function getCurrentTree(path) {
  const paths = path.split('.')
  return traverse(mainList, paths, 0) 
}

function traverse(list, paths, level) {
  const node = list.find(obj => obj.name === paths[level])

  if (level === paths.length - 1) {
    return node
  } else {
    return traverse(node.children, paths, level + 1)
  }
}

getCurrentTree("root.page")
// => {name: "page", children: Array(0)}