I've got object of filters that look like this:
var filterUpdate = {
assetClass: [{id: 1, name: 'photo'}, {id: 2, name: 'thumbnail'} ]
vehicleType: [{id: 1, name: 'nissan'}]
}
I've got state data like this (the keys representing years are dynamic and can change):
this.state.filteredOwnership = {
2020: [],
2021: [{assetClass: 'thumbnail', vehicleType: 'Toyota' }, {assetClass: 'Abs', vehicleType: 'Prius' }]
2022: [{assetClass: 'thumbnail', vehicleType: 'Toyota' }, {assetClass: 'photo', vehicleType: 'Prius' }, {assetClass: 'Abs', vehicleType: 'nissan'}, {assetClass: 'LMO', vehicleType: 'Tesla'} ]
}
This is the present lodash chain:
const filterData = _filter(filteredOwnership, (dataitem)=> {
if(_isEmpty(filterUpdate.assetClass) && _isEmpty(filterUpdate.vehicleType)) {
return dataitem
}
if(_some(_map(filterUpdate.assetClass, 'name'), dataitem.assetClass) || _some(_map(filterUpdate.vehicleType, 'name'), dataitem.vehicleType)){
return dataitem
}
});
We want to return a filtered array that would look like this:
filterData = {
2020: [],
2021: [{assetClass: 'thumbnail', vehicleType: 'Toyota' }]
2022: [{assetClass: 'thumbnail', vehicleType: 'Toyota' }, {assetClass: 'photo', vehicleType: 'Prius' }, {assetClass: 'Abs', vehicleType: 'nissan' } ]
}
The problem I'm having is that dataitem.assetClass and dataitem.vehicleType are the array of objects from within the year keys. I don't know where to use _map on dataitem that would help me return an item with in that array to be filtered
I am also open to non lodash suggestions! Regular js ways to do it would also be appreciated!