【问题标题】:Nested recursive array loop嵌套递归数组循环
【发布时间】:2016-02-19 03:49:44
【问题描述】:

想知道是否有人知道使用 lodash 或 vanilla JS 来解决这个小问题的方法?

我有这个起始对象:

{
  "1": {
    "null": {
      "2": {
        "3": {
          "6": {
            "7": "c"
          },
          "null": {
            "null": {
              "5": "b"
            }
          }
        }
      }
    }
  },
  "8": {
    "10": "e",
    "null": {
      "9": "d"
    }
  }
}

每个级别(水平)都意味着一些东西。所以级别 1 是 type A,级别 2 是 type B,3 是 type A,4 是 type B 等等。所以它交替出现。

有没有一种简单的方法可以“折叠”这个对象,使其看起来像这样:

[
  {
    "type": "A",
    "label": "1",
    "children": [
      {
        "type": "A",
        "label": "2",
        "children": [
          {
            "type": "B",
            "label": "3",
            "children": [
              {
                "type": "A",
                "label": "6",
                "children": [
                  {
                    "type": "A",
                    "label": "7",
                    "value": "c"
                  }
                ]
              },
              {
                "type": "A",
                "label": "8",
                "children": [
                  {
                    "type": "A",
                    "label": "5",
                    "value": "b"
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
  },
  {
    "type": "A",
    "label": "8",
    "children": [
      {
        "type": "B",
        "label": "10",
        "value": "e"
      },
      {
        "type": "A",
        "label": "9",
        "value": "d"
      }
    ]
  }
]

本质上用它是什么类型来注释每个级别,并嵌套它的子级。

【问题讨论】:

  • 您的文字描述表明您希望 type Atype B 交替使用。但是您提供的所需输出示例以不同的模式显示了类型,其中所有内容都是 type A,除了标签 310type B,这不是交替模式。您能否说明如何确定类型?
  • 另外,“null”标签似乎对您有特殊意义,您要跳过它吗?无论哪种方式,我都会说没有“简单的方法”或库函数可以做到这一点。您需要自己编写一个函数来执行此操作(或希望有人在这里发布)

标签: javascript recursion lodash


【解决方案1】:

这里是代码

function transformObj(obj, level) {
  level = level || 1;
  var result = _(obj).transform(function(result, value, key) {
    var obj = {
      type: (level % 2 === 0) ? 'B' : 'A',
      label: key
    };
    if (key === 'null') {
      result.push(transformObj(value, level+1));
    } else {
      if (_.isObject(value)) {
        obj.children = transformObj(value, level+1);
      } else {
        obj.value = value;
      }
      result.push(obj);
    }
  }, [])
  .flatten()
  .value();
  return result;
}

这是输出

[
    {
        "type": "A",
        "label": "1",
        "children": [
            {
                "type": "A",
                "label": "2",
                "children": [
                    {
                        "type": "B",
                        "label": "3",
                        "children": [
                            {
                                "type": "A",
                                "label": "6",
                                "children": [
                                    {
                                        "type": "B",
                                        "label": "7",
                                        "value": "c"
                                    }
                                ]
                            },
                            {
                                "type": "A",
                                "label": "5",
                                "value": "b"
                            }
                        ]
                    }
                ]
            }
        ]
    },
    {
        "type": "A",
        "label": "8",
        "children": [
            {
                "type": "B",
                "label": "10",
                "value": "e"
            },
            {
                "type": "A",
                "label": "9",
                "value": "d"
            }
        ]
    }
]

【讨论】:

    【解决方案2】:

    这应该可以解决问题:

    var source = {
      "1": {
        "null": {
          "2": {
            "3": {
              "6": {
                "7": "c"
              },
              "null": {
                "null": {
                  "5": "b"
                }
              }
            }
          }
        }
      },
      "8": {
        "10": "e",
        "null": {
          "9": "d"
        }
      }
    };
    
    
    
    function collapse(obj, parent, level){
    	var result = parent || [];
    	level = level || 0;
    	for(prop in obj){
    		var item = obj[prop];
    		var build = {
    						type : level % 2 ? "B" : "A", 
    						label : prop
    						//, level : level 
    					}
    		if(typeof item == 'object'){
    			build.children = [];
    			collapse(item, build.children, level + 1);
    		} else {
    			build.value = item;
    		}
    		
    		result.push(build);
    	}
    	return result;
    }
    
    var output = collapse(source);
    var result = JSON.stringify(output, null, ' ');
    console.log(result);
    
    var elem = document.getElementById("result");
    elem.innerHTML = result;
    <pre id="result"></pre>

    【讨论】:

      【解决方案3】:
      function doIt(data){
          return _.chain(data)
          .transform(function(result, value, key){
              if(key !== 'null'){
                  var type = _.parseInt(key) % 2 === 1 ? 'A' : 'B';
                  if(_.isObject(value) && !_.includes(_.keys(value), 'prop1')){
                      result.push({
                          type: type,
                          label: key,
                          children: doIt(value)
                      });
                  } else {
                      result.push({
                          type: type,
                          label: key,
                          value: value
                      });
                  }
              } else {
                  if(_.isObject(value)){
                      result.push(doIt(value));
                  }
              }
          }, [])
          .flatten()
          .value();
      }
      var result = doIt(data);
      

      结果是

      [
      {
          "type": "A",
          "label": "1",
          "children": [
              {
                  "type": "B",
                  "label": "2",
                  "children": [
                      {
                          "type": "A",
                          "label": "3",
                          "children": [
                              {
                                  "type": "B",
                                  "label": "6",
                                  "children": [
                                      {
                                          "type": "A",
                                          "label": "7",
                                          "value": "c"
                                      }
                                  ]
                              },
                              {
                                  "type": "A",
                                  "label": "5",
                                  "value": "b"
                              }
                          ]
                      }
                  ]
              }
          ]
      },
      {
          "type": "B",
          "label": "8",
          "children": [
              {
                  "type": "B",
                  "label": "10",
                  "value": "e"
              },
              {
                  "type": "A",
                  "label": "9",
                  "value": "d"
              }
          ]
      }
      ]
      

      【讨论】:

      • 这似乎可行 - 有没有办法扩展它,以便最终的 value 是一个对象本身?所以不是"value": "a" 我有"value": {"prop1": 1, "prop2": 2} 等等..?
      猜你喜欢
      • 1970-01-01
      • 2014-09-14
      • 1970-01-01
      • 1970-01-01
      • 2021-05-19
      • 1970-01-01
      • 2018-11-11
      • 2010-12-12
      • 1970-01-01
      相关资源
      最近更新 更多