【问题标题】:multilevel object traversing and print value based on key基于键的多级对象遍历和打印值
【发布时间】:2021-06-26 04:34:44
【问题描述】:

这是我的object。孩子里面可能有孩子等等。

我正在尝试只为所有这些打印name

这里是JSFiddle

var products = [
        {
        name: 'Allison',
        children: [
            {
                name: 'John',
                children: [
                    {
                        name: 'Scott',
                        children: [],
                    },
                ],
            },
            {
                name: 'Sarah',
                children: [],
            },
        ]
    },
    {
        name: 'Tony',
        children: [
            {
                name: 'Lucy',
                children: [],
            }
        ]
    }

这是我迄今为止尝试过的,我如何打印所有孩子的name,无论他们在对象中的哪个级别??

    for(var i = 0; i < products.length; i++)
    {
    console.log(products[i].name);
    
    if(products[i].children.length > 0) {
        console.log(products[i].children);
        // Print only name of all children.
        }
    }

【问题讨论】:

    标签: javascript json loops object


    【解决方案1】:

    您可以使用recursive技术。

    var products = [ { name: 'Allison', children: [{ name: 'John', children: [ {  name: 'Scott', children: [],},],},{ name: 'Sarah', children: [],},]},{name: 'Tony',children: [{name: 'Lucy',children: [],}]}];
        
    const printRecursively = (products) => {
        for (const k of products)
        {
            console.log(k.name);
            k.children.length > 0 &&  printRecursively(k.children); // Recurive here.
        }
    }
    
    printRecursively(products);

    【讨论】:

      【解决方案2】:

      这是实现,请检查 Also here

      var products = [
              {
              name: 'Allison',
              children: [
                  {
                      name: 'John',
                      children: [
                          {
                              name: 'Scott',
                              children: [],
                          },
                      ],
                  },
                  {
                      name: 'Sarah',
                      children: [],
                  },
              ]
          },
          {
              name: 'Tony',
              children: [
                  {
                      name: 'Lucy',
                      children: [],
                  }
              ]
          }
          ];
          
          
      function printChildrenNames(children) {
        for(var i = 0; i < children.length; i++) {
          console.log(children[i].name);
      
          if(children[i].children.length > 0) {
            printChildrenNames(children[i].children);
          }
        }
      }
      printChildrenNames(products)    

      【讨论】:

        【解决方案3】:

        这是使用object-scan 的迭代解决方案。对于您的用例,一个简单的递归解决方案可能是最佳选择,但是该解决方案非常简洁,并且在需求发生变化时易于调整。

        // const objectScan = require('object-scan');
        
        const data = [{ name: 'Allison', children: [{ name: 'John', children: [{ name: 'Scott', children: [] }] }, { name: 'Sarah', children: [] }] }, { name: 'Tony', children: [{ name: 'Lucy', children: [] }] }];
        
        console.log(objectScan(['**.name'], { rtn: 'value', reverse: false })(data));
        // => [ 'Allison', 'John', 'Scott', 'Sarah', 'Tony', 'Lucy' ]
        .as-console-wrapper {max-height: 100% !important; top: 0}
        &lt;script src="https://bundle.run/object-scan@14.0.0"&gt;&lt;/script&gt;

        免责声明:我是object-scan的作者

        【讨论】:

          猜你喜欢
          • 2021-12-16
          • 2019-05-06
          • 1970-01-01
          • 2013-05-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-08-30
          • 2011-08-03
          相关资源
          最近更新 更多