I have an array of objects with the following structure:
var items = [
{
attributes: [
{
type: "Size",
value: "Small"
},
{
type: "Color",
value: "Black"
}
]
},
{
attributes: [
{
type: "Size",
value: "Small"
},
{
type: "Color",
value: "White"
}
]
},
{
attributes: [
{
type: "Size",
value: "Medium"
},
{
type: "Color",
value: "Black"
}
]
},
{
attributes: [
{
type: "Size",
value: "Medium"
},
{
type: "Color",
value: "White"
}
]
}
];
My goal is to extract all the "value" property with the same "type". For example, the type "Size" has the following values: Small, Medium. After that, I want to create a new list with the following structure:
var results = [
{
type: "Size",
values: ["Small", "Medium"]
},
{
type: "Color",
values: ["White", "Black"]
}
];
This is my current code:
var results = items.map((item, index) => {
return item.attributes.map(attribute => {
let value = [];
value.push(attribute.value);
return {
type: attribute.type,
value: attribute.value
};
});
});