Within my array called objectArray
, I have an object with keys representing different types of clothes.
const objectArray: clothesTypes[] = [
{
trousers: 90,
'skirts and jackets': 47,
scarfs: 100,
},
]
I also have another array named userArray[]
, which keeps track of user data when purchasing clothes.
const userArray: userData[] = [
{
name: 'David',
clothType: 'trousers',
price: 45,
},
{
name: 'Joanna',
clothType: 'scarfs',
price: 5,
},
{
name: 'Gillian',
clothType: 'trousers',
price: 35,
},
]
The task at hand is to update the values of the keys in the objectArray
based on the number of objects with corresponding 'clothType' in the userArray
. For instance, trousers should have a value of 2, 'skirts and jackets' should be 0, and scarfs should be 1.
This is my attempted method:
objectArray.map(item => ({
...item,
...{
trousers: userArray.filter(d => d.clothType === 'trousers').length,
'skirts and jackets': userArray.filter(d => d.clothType === 'skirts and jackets').length,
scarfs: userArray.filter(d => d.clothType === 'scarfs').length,
},
})),
Is there a way to achieve this without manually assigning values to each key?
Thank you!