In the example I use a each to go thru select fields and create an array of data, I need to group 2 properties year and cabin also the property months is an array that I would like to merge, thing is I'm having a bit of trouble getting the wanted result:
JSFIDDLE: http://jsfiddle.net/tc39xu6b/2/
Result I get:
[
{
"year": "2021",
"cabin": "1",
"months": [
"1",
"2"
]
},
{
"year": "2021",
"cabin": "1",
"months": [
"4"
]
},
{
"year": "2021",
"cabin": "2",
"months": [
"1",
"2"
]
},
{
"year": "2022",
"cabin": "1",
"months": [
"1",
"2"
]
},
{
"year": "2022",
"cabin": "1",
"months": [
"4"
]
},
{
"year": "2022",
"cabin": "2",
"months": [
"1",
"2"
]
}
]
and this is the result I need:
{
"2021": [
{"cabin":1, "months": ["1", "2","4"]},
{"cabin":2, "months": ["1", "2"]}
],
"2022": [
{"cabin":1, "months": ["1", "2","4"]},
{"cabin":2, "months": ["1", "2"]}
]
}
let obj = {}, thenobj[year] = [{'cabin': cabin, 'months': months}];in your loop