With Javascript, I want to split an array and group by an attribute
input:
[
{
"type": "typeA",
"label": "labelA",
"placeholders": [
"b",
"a",
"r"
]
},{
"type": "typeB",
"label": "labelB",
"placeholders": [
"x",
"y",
"z"
]
},{
"type": "typeA",
"label": "labelAAA",
"placeholders": [
"a",
"b",
"c"
]
}
]
I want output:
[
{
"type": "typeA",
"items": [
{
"label": "labelA",
"placeholders": [
"b",
"a",
"r"
]
},
{
"label": "labelAAA",
"placeholders": [
"a",
"b",
"c"
]
}
]
},
{
"type": "typeB",
"items": [
{
"label": "labelB",
"placeholders": [
"b",
"a",
"r"
]
}
]
}
]
I think javascript use .reduce
, .concat
, .map
, ...
I'd like to do this without additional libraries (e.g., lodash).