将2D阵列变成树

时间:2019-02-11 11:25:52

标签: javascript

我正在使用Google Apps脚本,并且可以将Google Spreadsheet Data检索为2D数组。我想将此数组转换为树表示形式,以在Google云端硬盘中构建文件夹层次结构。 enter image description here

这是我可以从Google Spreadsheet获得的2D数组。

var data =  [
       ["F1", "F1S1","",""],
       ["", "F1S2", "F1S2S1", ""],
       ["", "", "F1S2S2", ""],
       ["", "F1S3", "", ""],
       ["F2", "F2S1", "", ""],
       ["", "F2S2", "F2S2S1", ""],
       ["", "", "F2S2S2", "F2S2S2S1"],
       ["", "", "", "F2S2S2S2"]
    ]

现在我正尝试将其转换为JSON以下。

[
    {
        "name" : "F1",
        "children" : [
            {
                "name" : "F1S1"
            },
            {
                "name" : "F1S2",
                "children" : [
                    {
                        "name" : "F1S2S1"
                    },
                    {
                        "name" : "F1S2S2"
                    }
                ]
            },
            {
                "name" : "F1S3"
            }
        ]
    },
    {
        "name" : "F2",
        "children" : [
            {
                "name" : "F2S1"
            },
            {
                "name" : "F2S2",
                "children" : [
                    {
                        "name" : "F2S2S1"
                    },
                    {
                        "name" : "F2S2S2",
                        "children" : [
                            {
                                "name" : "F2S2S2S1"
                            },
                            {
                                "name" : "F2S2S2S2"
                            }
                        ]
                    }
                ]
            }
        ]
    }
]

我尝试使用array.reduce方法,但无法推断出正确的逻辑。 有人可以引导我。

var data = [
  ["F1", "F1S1", "", ""],
  ["", "F1S2", "F1S2S1", ""],
  ["", "", "F1S2S2", ""],
  ["", "F1S3", "", ""],
  ["F2", "F2S1", "", ""],
  ["", "F2S2", "F2S2S1", ""],
  ["", "", "F2S2S2", "F2S2S2S1"],
  ["", "", "", "F2S2S2S2"]
]

var tree = data.reduce(function(tree, item) {
  var tempTree = tree;
  for (var i = 0; i < item.length; i++) {
    if (!tempTree[item[i]])
      tempTree[item[i]] = {};
    tempTree = tempTree[item[i]];
  }
  return tree;
}, {});

console.log(tree)

1 个答案:

答案 0 :(得分:7)

使用排序后的数据,您可以使用一个辅助数组来保留插入节点的最后一级。

var data = [["F1", "F1S1", "", ""], ["", "F1S2", "F1S2S1", ""], ["", "", "F1S2S2", ""], ["", "F1S3", "", ""], ["F2", "F2S1", "", ""], ["", "F2S2", "F2S2S1", ""], ["", "", "F2S2S2", "F2S2S2S1"], ["", "", "", "F2S2S2S2"]],
    result = [],
    levels = [result];

data.forEach(a =>
    a.forEach((name, i) => {
        if (!name) return;
        levels[i].push({ name, children: (levels[i + 1] = []) });
    })
);
  
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

如果您不希望有空子节点,则可以在添加新节点之前预先检查并添加子节点。

var data = [["F1", "F1S1", "", ""], ["", "F1S2", "F1S2S1", ""], ["", "", "F1S2S2", ""], ["", "F1S3", "", ""], ["F2", "F2S1", "", ""], ["", "F2S2", "F2S2S1", ""], ["", "", "F2S2S2", "F2S2S2S1"], ["", "", "", "F2S2S2S2"]],
    result = [],
    levels = [result];

data.forEach(a =>
    a.forEach((name, i) => {
        if (!name) return;
        if (i && !levels[i - 1][levels[i - 1].length - 1].children) {
            levels[i] = [];
            levels[i - 1][levels[i - 1].length - 1].children = levels[i];
        }
        levels[i].push({ name });
    })
);
  
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }