I need to make an object with counters for every type of houseType, from an array of objects; idea is for the keys and properties to be dynamic in order to account for potential new houseType's.
From:
[{
id: 1,
name: 'House sth',
desc: 'lorem ipsum',
type: 'house'
},
{
id: 2,
name: 'Building sth',
desc: 'lorem ipsum',
type: 'building'
},
{
id: 3,
name: 'House two',
desc: 'lorem ipsum',
type: 'house'
}]
To this result:
{ house: 2, building: 1 } (two of type === 'house' and one of type === 'building')
The next was my approach, but of course the values are NaN:
useEffect(() => {
listings.map((listing, index) => {
setPropertyCount(prevState => ({
...prevState,
[listing.type]: prevState[listing.type] + 1,
}));
});
}, []);