How can I change the data structure if there are identical types in the keys?
I get a data structure like this, is it normal practice to change the data on the frontend side?
I want to bring the general type to the top level and merge the rest of the data.
Data:
const data = [
{
id: 0,
type: "type1",
name: "",
active: [],
products: [{}],
documents: [{}],
},
{
id: 1,
type: "type1",
name: "",
active: [],
documents: [{}],
products: [{}],
},
{
id: 2,
type: "type2",
name: "",
active: [],
documents: [{}],
products: [],
}
]
Result:
const result = [
{
type: 'type1',
merged: [
{
id: 0,
name: '',
active: [],
products: [{}],
documents: [{}],
},
{
id: 1,
name: '',
active: [],
products: [{}],
documents: [{}],
},
],
},
{
type: 'type2',
merged: [
{
id: 2,
name: '',
active: [],
products: [{}],
documents: [{}],
},
],
},
];