将多个键上的JSON数据分组并保留键

时间:2019-04-26 19:32:12

标签: javascript lodash

我有一个扁平的JSON文件,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.bottomappbar.BottomAppBar
        android:id="@+id/bottom_app_bar"
        style="@style/Widget.MaterialComponents.BottomAppBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        app:backgroundTint="@color/colorGray"
        app:fabAlignmentMode="center" />

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom">

        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/nav_view"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_gravity="bottom"
            android:background="#80FFFFFF"
            android:icon="@drawable/bottom_nav_ic_assignment"
            app:itemIconTint="@color/bottom_nav_item_color"
            app:itemTextColor="@color/bottom_nav_item_color"
            app:labelVisibilityMode="selected"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:menu="@menu/bottom_nav_menu" />

    </FrameLayout>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/addFab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="true"
        android:onClick="onFabClicked"
        android:src="@drawable/ic_add_white"
        app:backgroundTint="@color/colorBlue"
        app:fabSize="auto"
        app:layout_anchor="@+id/bottom_app_bar"
        app:layout_anchorGravity="center|top" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

我需要按页码,按页面版本,按PtrChild,按分配对数据进行分组,以便在完成后看起来像这样:

{"dataSet": {"dataTable": [{
    "id": "List1",
    "row": [
        {
            "pagenumber": "7",
            "pageversion": "HE; K12",
            "category": "3D Print - K12;HE",
            "pagetype": "Product",
            "PtrChild": "MakerBot - 10771",
            "Allocation": "0.500",
            "catext": "Text goes here"
        },
        {
            "pagenumber": "7",
            "pageversion": "SL",
            "category": "3D Print - SL",
            "pagetype": "Product",
            "PtrChild": "AUTODESK - 10032",
            "Allocation": "0.500",
            "catext": "Text goes here"
        },
        {
            "pagenumber": "10",
            "pageversion": "Apply to All",
            "category": "Secure Printers",
            "pagetype": "Brand",
            "PtrChild": null,
            "Allocation": "1.000",
            "catext": "Text goes here"
        },
        {
            "pagenumber": "11",
            "pageversion": "Apply to All",
            "category": "Secure Printers",
            "pagetype": "Product",
            "PtrChild": "EPSON INK JET** - 10082",
            "Allocation": "0.200",
            "catext": "Text goes here"
        },
        {
            "pagenumber": "11",
            "pageversion": "Apply to All",
            "category": "Secure Printers",
            "pagetype": "Product",
            "PtrChild": "EPSON INK JET** - 10082",
            "Allocation": "0.200",
            "catext": "Text goes here"
        },
        {
            "pagenumber": "11",
            "pageversion": "Apply to All",
            "category": "Secure Printers",
            "pagetype": "Product",
            "PtrChild": "EPSON INK JET** - 10082",
            "Allocation": "0.500",
            "catext": "Text goes here"
        },
        {
            "pagenumber": "11",
            "pageversion": "Apply to All",
            "category": "Secure Printers",
            "pagetype": "Product",
            "PtrChild": "LEXMARK** - 10151",
            "Allocation": "0.200",
            "catext": "Text goes here"
        }
    ]
}]}}

运行此:

{"pages": [
    {
        "pagenumber": "7",
        "versions": [
            {
                "pageversion": "HE; K12",
                "category": "3D Print - K12;HE",
                "pagetype": "Product",
                "partners": [{
                    "PtrChild": "MakerBot - 10771",
                    "allocations": [{
                        "Allocation": "0.500",
                        "ads": [{"catext": "Text goes here"}]
                    }]
                }]
            },
            {
                "pageversion": "SL",
                "category": "3D Print - SL",
                "pagetype": "Product",
                "partners": [{
                    "PtrChild": "AUTODESK - 10032",
                    "allocations": [{
                        "Allocation": "0.500",
                        "ads": [{"catext": "Text goes here"}]
                    }]
                }]
            }
        ]
    },
    {
        "pagenumber": "10",
        "versions": [{
            "pageversion": "Apply to All",
            "category": "Secure Printers",
            "pagetype": "Brand",
            "partners": [{
                "PtrChild": null,
                "allocations": [{
                    "Allocation": "1.500",
                    "ads": [{"catext": "Text goes here"}]
                }]
            }]
        }]
    },
    {
        "pagenumber": "11",
        "versions": [{
            "pageversion": "Apply to All",
            "category": "Secure Printers",
            "pagetype": "Product",
            "partners": [
                {
                    "PtrChild": "EPSON INK JET** - 10082",
                    "allocations": [
                        {
                            "Allocation": "0.250",
                            "ads": [
                                {"catext": "Text goes here"},
                                {"catext": "Text goes here"}
                            ]
                        },
                        {
                            "Allocation": "0.500",
                            "ads": [{"catext": "Text goes here"}]
                        }
                    ]
                },
                {
                    "PtrChild": "LEXMARK** - 10151",
                    "allocations": [{
                        "Allocation": "0.200",
                        "ads": [{"catext": "Text goes here"}]
                    }]
                }
            ]
        }]
    }
]}

...获得正确的分组,但消除了每个组的键:

var myGroupedData = nest(myCatalog, ["pagenumber", "pageversion", "PtrChild", "Allocation"]);
// Reorganize JSON data
function nest(collection, keys) {
    if (!keys.length) {
        return collection;
    }
    else {
        return _(collection).groupBy(keys[0]).mapValues(function(values) { 
            return nest(values, keys.slice(1));
        }).value();
    }
}

如何保留键并在多个键上分组?

我被迫使用ExtendScript,因此不能使用3.10.1版以后的Lodash。

2 个答案:

答案 0 :(得分:2)

您可以为每个级别的期望属性获取一个辅助数组,为不同名称的children数组获取一个最后一项,并为对象提取使用过的属性以获取最终清理的对象的方法。

var data = { dataSet: { dataTable: [{ id: "List1", row: [{ pagenumber: "7", pageversion: "HE; K12", category: "3D Print - K12;HE", pagetype: "Product", PtrChild: "MakerBot - 10771", Allocation: "0.500", catext: "Text goes here" }, { pagenumber: "7", pageversion: "SL", category: "3D Print - SL", pagetype: "Product", PtrChild: "AUTODESK - 10032", Allocation: "0.500", catext: "Text goes here" }, { pagenumber: "10", pageversion: "Apply to All", category: "Secure Printers", pagetype: "Brand", PtrChild: null, Allocation: "1.000", catext: "Text goes here" }, { pagenumber: "11", pageversion: "Apply to All", category: "Secure Printers", pagetype: "Product", PtrChild: "EPSON INK JET** - 10082", Allocation: "0.200", catext: "Text goes here" }, { pagenumber: "11", pageversion: "Apply to All", category: "Secure Printers", pagetype: "Product", PtrChild: "EPSON INK JET** - 10082", Allocation: "0.200", catext: "Text goes here" }, { pagenumber: "11", pageversion: "Apply to All", category: "Secure Printers", pagetype: "Product", PtrChild: "EPSON INK JET** - 10082", Allocation: "0.500", catext: "Text goes here" }, { pagenumber: "11", pageversion: "Apply to All", category: "Secure Printers", pagetype: "Product", PtrChild: "LEXMARK** - 10151", Allocation: "0.200", catext: "Text goes here" }] }] } },
    props = [["pagenumber", "versions"], ["pageversion", "category", "pagetype", "partners"], ["PtrChild", "allocations"], ["Allocation", "ads"]],
    result = data.dataSet.dataTable.reduce((r, { row }) => {
        row.forEach(o => props.reduce((a, [...keys]) => {
            var children = keys.pop(),
                temp = a.find(p => p[keys[0]] === o[keys[0]]);
            if (!temp) a.push(temp = Object.assign(...keys.map(k => ({ [k]: o[k] })), { [children]: [] }));
            o = keys.reduce((p, k) => ({ [k]: k, ...p } = p, p), o);
            return temp[children];
        }, r).push(o));
        return r;
    }, []);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 1 :(得分:0)

此解决方案适用于ES5和lodash 3.10.1。它基于Nina Scholz's answer中的道具思想。

但是,这里的想法不是从最终对象中删除属性,而是让每个级别在组之后选择其自己的属性,如果是最后一个级别,请避免进行分组,并从这些属性创建一个数组。

function nest(collection, props) { 
  var currentProps = _.first(props);
  var restProps = _.rest(props);
  
  if(_.size(props) === 1) {
    return _.map(collection, _.partialRight(_.pick, currentProps));
  }  

  return _(collection)
    .groupBy(_.first(currentProps))
    .map(function(values) {
      var item = _.first(values);
      var fields = _.pick(item, _.initial(currentProps));
      var collectionKey = _.last(currentProps);
      fields[collectionKey] = nest(values, restProps);
    
      return fields;
    })
    .value();
}

var myCatalog = {"dataSet":{"dataTable":[{"id":"List1","row":[{"pagenumber":"7","pageversion":"HE; K12","category":"3D Print - K12;HE","pagetype":"Product","PtrChild":"MakerBot - 10771","Allocation":"0.500","catext":"Text goes here"},{"pagenumber":"7","pageversion":"SL","category":"3D Print - SL","pagetype":"Product","PtrChild":"AUTODESK - 10032","Allocation":"0.500","catext":"Text goes here"},{"pagenumber":"10","pageversion":"Apply to All","category":"Secure Printers","pagetype":"Brand","PtrChild":null,"Allocation":"1.000","catext":"Text goes here"},{"pagenumber":"11","pageversion":"Apply to All","category":"Secure Printers","pagetype":"Product","PtrChild":"EPSON INK JET** - 10082","Allocation":"0.200","catext":"Text goes here"},{"pagenumber":"11","pageversion":"Apply to All","category":"Secure Printers","pagetype":"Product","PtrChild":"EPSON INK JET** - 10082","Allocation":"0.200","catext":"Text goes here"},{"pagenumber":"11","pageversion":"Apply to All","category":"Secure Printers","pagetype":"Product","PtrChild":"EPSON INK JET** - 10082","Allocation":"0.500","catext":"Text goes here"},{"pagenumber":"11","pageversion":"Apply to All","category":"Secure Printers","pagetype":"Product","PtrChild":"LEXMARK** - 10151","Allocation":"0.200","catext":"Text goes here"}]}]}};

var props = [["pagenumber", "versions"], ["pageversion", "category", "pagetype", "partners"], ["PtrChild", "allocations"], ["Allocation", "ads"], ["catext"]];

var myGroupedData = nest(_.flatten(_.map(myCatalog.dataSet.dataTable, 'row')), props);

console.log(myGroupedData);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.js"></script>