我正在努力订购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
},
]
}
这对我来说非常复杂,所以我需要你的帮助。 你有什么想法吗?
请不要限制你的想法w / underscore.js。
答案 0 :(得分:1)
我最近必须做类似的事情,我使用了Underscore.nest(https://github.com/iros/underscore.nest),这是一个简洁的小js插件,你可以指定一个字段来折叠数据,然后你可以使用underscore.sort对生成的树结构进行排序?
答案 1 :(得分: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
数组中(当您添加数组时)取决于你),然后,也许在你搜索或之后,过滤掉所有已经分配了父母的对象(不是他们自己)。