From what I understand, your input format is similar to:
var seatsPaxAirline =
[{key: "5Y", value: {pax: 60, seats: 100}},
{key: "5Y", value: {pax: 40, seats: 100}},
{key: "3Y", value: {pax: 30, seats: 100}},
{key: "2Y", value: {pax: 70, seats: 100}},
{key: "4Y", value: {pax: 50, seats: 100}},
{key: "1Y", value: {pax: 45, seats: 100}},
{key: "2Y", value: {pax: 50, seats: 100}},
{key: "5Y", value: {pax: 60, seats: 100}},
{key: "5Y", value: {pax: 65, seats: 100}},
{key: "5Y", value: {pax: 55, seats: 100}}
];
And, you wish to group elements based on their key values like:
{
"5Y": [{"key": "5Y", "value": {"pax": 60, "seats": 100}},
{"key": "5Y", "value": {"pax": 40, "seats": 100}},
{"key": "5Y", "value": {"pax": 60, "seats": 100}},
{"key": "5Y", "value": {"pax": 65, "seats": 100}},
{"key": "5Y", "value": {"pax": 55, "seats": 100}}],
"3Y": [{"key": "3Y", "value": {"pax": 30, "seats": 100}}],
"2Y": [{"key": "2Y", "value": {"pax": 70, "seats": 100}},
{"key": "2Y", "value": {"pax": 50, "seats": 100}}],
"4Y": [{"key": "4Y", "value": {"pax": 50, "seats": 100}}],
"1Y": [{"key": "1Y", "value": {"pax": 45, "seats": 100}}]
}
If this is what you are looking to achieve using D3 then this is the way to do it:
d3.nest().key(function(d){return d.key}).map(seatsPaxAirline);
And, here's the working demo.
EDIT:
I believe the sample object you've provided is malformed. But, if you modify the code I provided as shown below:
var output = d3.nest().key(function(d){return d.key}).map(seatsPaxAirline);
d3.entries(d3.entries(output).forEach(function(d,i){
d.value.forEach(function(o,j){
delete o["key"];
d.value[j] = d3.entries(o.value);
});
}));
output = d3.entries(output);
The output you'll get is:
[{
"key": "5Y",
"value": [
[{"key": "pax","value": 60}, {"key": "seats","value": 100}],
[{"key": "pax","value": 40}, {"key": "seats","value": 100}],
[{"key": "pax","value": 60}, {"key": "seats","value": 100}],
[{"key": "pax","value": 65}, {"key": "seats","value": 100}],
[{"key": "pax","value": 55}, {"key": "seats","value": 100}]
]
}, {
"key": "3Y",
"value": [
[{"key": "pax","value": 30}, {"key": "seats","value": 100}]
]
}, {
"key": "2Y",
"value": [
[{"key": "pax","value": 70}, {"key": "seats","value": 100}],
[{"key": "pax","value": 50}, {"key": "seats","value": 100}]
]
}, {
"key": "4Y",
"value": [
[{"key": "pax","value": 50}, {"key": "seats","value": 100}]
]
}, {
"key": "1Y",
"value": [
[{"key": "pax","value": 45}, {"key": "seats","value": 100}]
]
}]
This is as close as I can get it to the desired data structure. See if this is usable for you.
Updated Demo
PS: I realize that this isn't the most efficient of solutions but this is as good as it gets with me & D3. If you wish to fool around with the data structure, consider using underscoreJS.