Reputation: 53
I'm struggling with ordering JavaScript objects. Here is the part of data that I have to sort.
I would like to group each object by ParentMemberCode
and then order them by 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
}
I want to restructure and sort the data like this:
{
"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
},
]
}
It's really complex to me so I need your help. Do you have any ideas about that?
Please don't limit your idea w/ underscore.js.
Upvotes: 2
Views: 341
Reputation: 7141
I was about to say I don't see what the big problem is here. But then I realize what you wanted is invalid.
You can't have an item in an object without a key.
{
"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
},
....
does not create a valid object. you'd need a name for the property holding the array.
{
"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
},
....
As far as creating this, you only need to be able to iterate through your objects in the topmost array, looking up each ParentMemberCode
by it's MemberCode
(you might want to create another dictionary referring to each object by the MemberCode
directly before you iterate through so that each lookup takes on average log(the number of items)), and add the child item to the parent's Children
array (when you add the array is up to you), and then, maybe as you search or afterwards, filter out all the objects that have already been assigned a parent (that isn't themselves).
Upvotes: 1
Reputation: 1004
I had to do something similar recently, I used Underscore.nest (https://github.com/iros/underscore.nest), which is a neat little js plugin where you can specify a field to fold the data into, You can then use underscore.sort to sort the generated tree structure?
Upvotes: 1