I am getting data like below :
data.filter = {
selectedFilterItems: [
{
name: "0-435",
categoryId: "0_435",
code: "price",
totalItems: 1,
},
{
name: "MULTI",
categoryId: "7608",
count: 633,
code: "color",
subcategories: [],
totalItems: 1,
},
{
name: "Pastel Tie Dye",
categoryId: "566962",
count: 1,
code: "color",
subcategories: [],
totalItems: 1,
},
],
totalItems: 3,
};
I need to create a json like below, where type will be the "code" i get from above data (i.e data.filter) & the value will be comma seperated for common code.
Desired Output :
filters: [
{
type: 'price',
value: '0-435'
}
]
exg for color there will be only one type = color & its value will be "MULTI,Pastel Tie Dye".
if only one distinct code is there in data.filter then only that data will be in filters.
Here is the code which i am trying
if(data.filter) {
const selectedFilterItems = data.filter.selectedFilterItems.reduce((property, attribute) => {
if (property[attribute.code]) {
property[attribute.code].value += `,${attribute.name}`;
} else {
property[attribute.code] = { type: attribute.code, value: attribute.name };
}
return property;
}, {});
filter_data = Object.values(selectedFilterItems);
}
i need to add to the "filter_data" variable.
{
type: 'category',
value: '7608' // where the value will be there "selectedFilterItems's any of the property's categoryId's value.
}