I have below array
const array = [
{
key: 'TSJUcgLGig2UOdjAzlqq4KW5GK6PKXHC2ndw40HYIss7EKsV6nJ',
location: { lat: '22.7196', lng: '75.8577' },
address: { city: 'NY', country: 'USA' },
className: 'FourthActivity',
deviceId: '1111111111111111'
},
{
key: 'TSJUcgLGig2UOdjAzlqq4KW5GK6PKXHC2ndw40HYIss7EKsV6nJ',
location: { lat: '22.7196', lng: '75.8577' },
address: { city: 'NY', country: 'USA' },
className: 'FourthActivity',
deviceId: '222222222222222'
},
{
key: 'TSJUcgLGig2UOdjAzlqq4KW5GK6PKXHC2ndw40HYIss7EKsV6nJ',
location: { lat: '22.7196', lng: '75.8577' },
address: { city: 'Landon', country: 'UK' },
className: 'FourthActivity',
deviceId: '33333333333'
}
]
I have to group it by using two properties inside the object i.e. address
and className
. How can I do this either with loadash or javascript?
expected output
[{
address: { city: 'NY', country: 'USA' }, className: 'FourthActivity',
count: 2,
data: [{
deviceId: 222222222222222,
}, {
deviceId: 1111111111111111,
}]
}, {
address: { city: 'Landon', country: 'UK' }, className: 'FourthActivity',
count: 1,
data: [{
deviceId: 33333333333,
}]
}]
I have tried this but doesn't work
var selectedVehicle = _.filter(array, 'address')
const selectedVehicles = _.groupBy(selectedVehicle, function(item) {
return item.makeCode;
})
console.log(selectedVehicles)