2

我正在努力订购 JavaScript 对象。这是我必须排序的数据部分。

我想将每个对象分组ParentMemberCode,然后按Order.

{
    "MemberCode": "B1G",
    "ParentMemberCode": "B1_GA",
    "MemberName": "Gross value added at basic prices, total activity",
    "Order": 1
},
{
    "MemberCode": "P119",
    "ParentMemberCode": "B1_GA",
    "MemberName": "Financial Intermediation Services Indirectly Measured (FISIM)",
    "Order": 2
},
{
    "MemberCode": "B1G_P119",
    "ParentMemberCode": "B1_GA",
    "MemberName": "Gross value added at basic prices, excluding FISIM",
    "Order": 3
},
{
    "MemberCode": "D21_D31",
    "ParentMemberCode": "B1_GA",
    "MemberName": "Taxes less subsidies on products",
    "Order": 4
},
{
    "MemberCode": "B1_GE",
    "ParentMemberCode": "GDP",
    "MemberName": "Gross domestic product - expenditure approach",
    "Order": 1
},
{
    "MemberCode": "GDP",
    "ParentMemberCode": "GDP",
    "MemberName": "Gross domestic product",
    "Order": 1
},
{
    "MemberCode": "B1_GI",
    "ParentMemberCode": "GDP",
    "MemberName": "Gross domestic product - income approach",
    "Order": 2
},
{
    "MemberCode": "B1_GA",
    "ParentMemberCode": "GDP",
    "MemberName": "Gross domestic product at market prices - output approach",
    "Order": 3
}

我想像这样重组和排序数据:

{
    "MemberCode": "GDP",
    "ParentMemberCode": "GDP",
    "MemberName": "Gross domestic product",
    "Order": 1
}

{
    "MemberCode": "B1_GE",
    "ParentMemberCode": "GDP",
    "MemberName": "Gross domestic product - expenditure approach",
    "Order": 1
},
,
{
    "MemberCode": "B1_GI",
    "ParentMemberCode": "GDP",
    "MemberName": "Gross domestic product - income approach",
    "Order": 2
},
{
    "MemberCode": "B1_GA",
    "ParentMemberCode": "GDP",
    "MemberName": "Gross domestic product at market prices - output approach",
    "Order": 3,
    [
        {
            "MemberCode": "B1G",
            "ParentMemberCode": "B1_GA",
            "MemberName": "Gross value added at basic prices, total activity",
            "Order": 1
        },
        {
            "MemberCode": "P119",
            "ParentMemberCode": "B1_GA",
            "MemberName": "Financial Intermediation Services Indirectly Measured (FISIM)",
            "Order": 2
        },
        {
            "MemberCode": "B1G_P119",
            "ParentMemberCode": "B1_GA",
            "MemberName": "Gross value added at basic prices, excluding FISIM",
            "Order": 3
        },
        {
            "MemberCode": "D21_D31",
            "ParentMemberCode": "B1_GA",
            "MemberName": "Taxes less subsidies on products",
            "Order": 4
        },
    ]
}

这对我来说真的很复杂,所以我需要你的帮助。你对此有什么想法吗?

请不要使用 underscore.js 限制您的想法。

4

2 回答 2

1

我正要说我看不出这里有什么大问题。但后来我意识到你想要的东西是无效的。

没有键,您不能在对象中拥有项目。

{
    "MemberCode": "B1_GA",
    "ParentMemberCode": "GDP",
    "MemberName": "Gross domestic product at market prices - output approach",
    "Order": 3,
    [
        {
            "MemberCode": "B1G",
            "ParentMemberCode": "B1_GA",
            "MemberName": "Gross value added at basic prices, total activity",
            "Order": 1
        },
....

不创建有效对象。你需要一个持有数组的属性的名称。

{
    "MemberCode": "B1_GA",
    "ParentMemberCode": "GDP",
    "MemberName": "Gross domestic product at market prices - output approach",
    "Order": 3,
    "Children": [
        {
            "MemberCode": "B1G",
            "ParentMemberCode": "B1_GA",
            "MemberName": "Gross value added at basic prices, total activity",
            "Order": 1
        },
....

就创建它而言,您只需要能够遍历最顶层数组中的ParentMemberCode对象,逐个查找MemberCode每个对象(您可能希望MemberCode在迭代之前直接创建另一个字典来引用每个对象,以便每次查找平均日志(项目数)),并将子项目添加到父Children数组(添加数组时由您决定),然后,可能在您搜索时或之后,过滤掉所有具有已经被分配了一个父母(不是他们自己)。

于 2013-07-10T15:34:15.093 回答
1

我最近不得不做类似的事情,我使用了 Underscore.nest ( https://github.com/iros/underscore.nest ),这是一个简洁的小 js 插件,您可以在其中指定一个字段来折叠数据,您可以然后使用 underscore.sort 对生成的树结构进行排序?

于 2013-07-10T15:27:29.210 回答