更好的解决对象阵列的问题

时间:2016-08-08 07:40:53

标签: javascript arrays tree flatten

对于以下对象数组

[
 {"Corp": "ABC", "T1": "HW A/V", "T2": "A/V System","T3": "Audio","Prod": "Audio System","Mfr": "One"},
 {"Corp": "ABC", "T1": "HW A/V", "T2": "A/V System","T3": "Audio","Prod": "Audio System","Mfr": "Two"},
 {"Corp": "ABC", "T1": "HW A/V", "T2": "A/V System","T3": "Video","Prod": "Video System","Mfr": "Other"}
]

我打算得到一个如下所示的不平坦的对象:

[{
"Corp": "ABC",
"List": [{
    "T1": "HW A/V",
    "List": [{
        "T2": "A/V System",
        "List": [{
            "T3": "Audio",
            "List": [{
                "Prod": "Audio System",
                "List": [
                    {"Mfr": "One"},
                    {"Mfr": "Two"}
                ]
            }]
        },
        {
            "T3": "Video",
            "List": [{
                "Prod": "Video System",
                "List": [
                    {"Mfr": "Other"}
                ]
            }]
        }]
    }]
}]

}] 我确实得到了我打算如上所述获得的结果。我用下划线来得到结果。以下代码片段为我完成了这项工作:

var items = _.map(_.groupBy(itemList, 'Corp'), function (a) {
        return _.extend(_.pick(a[0], 'Corp'), {
            List: _.map(_.groupBy(a, 'T1'), function (b) {
                return _.extend(_.pick(b[0], 'T1'), {
                    List: _.map(_.groupBy(b, 'T2'), function (c) {
                        return _.extend(_.pick(c[0], 'T2'), {
                            List: _.map(_.groupBy(c, 'T3'), function (d) {
                                return _.extend(_.pick(d[0], 'T3'), {
                                    List: _.map(_.groupBy(d, 'Prod'), function (e) {
                                        return _.extend(_.pick(e[0], 'Prod'), {
                                            List: _.map(e, function (elem) {
                                                return _.pick(elem, 'Mfr')
                                            })
                                        });
                                    })
                                });
                            })
                        });
                    })
                });
            })
        });
    });

现在,我正在寻找的是,如果有人可以增强我的解决方案。我想为这个过程优化空间和时间。

PS:早上,我问了一个类似的问题,要求提供解决方案,这个问题被标记为 TOO BROAD 并被置于 HOLD < / strong>,所以我已经用这个问题添加了我的解决方案,现在我正在寻找的是一个更好的解决方案。

由于

1 个答案:

答案 0 :(得分:2)

为了避免使用符号化的语句,您可能需要定义一个“扩展键”列表。并迭代它们。

通过以下方式自动提取密钥是半诱人的:

expandKeys = _.keys(itemList[0]);

但是由于Javascript并不能保证对象中键的顺序,所以你真的应该明确地定义这个列表。

下面是一些示例代码。

&#13;
&#13;
var itemList = [
  {"Corp": "ABC", "T1": "HW A/V", "T2": "A/V System", "T3": "Audio", "Prod": "Audio System", "Mfr": "One"},
  {"Corp": "ABC", "T1": "HW A/V", "T2": "A/V System", "T3": "Audio", "Prod": "Audio System", "Mfr": "Two"},
  {"Corp": "ABC", "T1": "HW A/V", "T2": "A/V System", "T3": "Video", "Prod": "Video System", "Mfr": "Other"}
];

var expandKeys = [ 'Corp', 'T1', 'T2', 'T3', 'Prod', 'Mfr' ];

function expandList(list, keys) {
  var node, obj, root = {};

  _.each(list, function(item) {
    obj = root;
    _.each(keys, function(key) {
      obj = (obj.List = obj.List || []);
      node = _.find(obj, function(i) { return i[key] == item[key]; });

      if(node === undefined) {
        obj.push(node = {});
        node[key] = item[key];
      }
      obj = node;
    });
  });
  return root.List;
}

var res = expandList(itemList, expandKeys);
console.log(res);
&#13;
<script src="http://underscorejs.org/underscore-min.js"></script>
&#13;
&#13;
&#13;