I have array terms:
[
{
"label": "programming",
"id": 6,
"taxonomy": "category"
},
{
"label": "design",
"id": 4,
"taxonomy": "post_tag"
},
{
"label": "css",
"id": 3,
"taxonomy": "post_tag"
},
{
"label": "other",
"id": 8,
"taxonomy": "category"
}
]
I am trying to create the following newArray array from array terms:
[
{
"taxonomy": "category",
"ids": [6, 8]
},
{
"taxonomy": "post_tag",
"ids": [4, 3]
},
]
I have to take the id from each object in the terms array and add them to the corresponding taxonomy in the array newArray.
I am trying the following:
const taxonomyArray = [];
const newArray = [];
// Loop through original term array and get the taxonomies
terms.forEach( el => {
taxonomyArray.push( el['taxonomy'] )
});
// Remove duplicate taxonomy values from array taxonomyArray
const uniqueTaxonomyArray = [ ...new Set(taxonomyArray) ];
// Create the new array
uniqueTaxonomyArray.forEach( el => {
const groupedArray = terms
.filter( ( { taxonomy } ) => taxonomy === el )
.map( ( { value, taxonomy } ) => ( {
taxonomy: el,
ids: value
} ) );
newArray.push( groupedArray );
});
It is giving me:
[
[
{
"taxonomy": "category",
"ids": 6
},
{
"taxonomy": "category",
"ids": 8
}
],
[
{
"taxonomy": "post_tag",
"ids": 4
},
{
"taxonomy": "post_tag",
"ids": 3
}
]
]
Is there a better way to achieve this?
flatMapinstead offorEach