Grouping within arraythen converting it to object

Thank you, but my version is mongoDB 4.2.8 and hence I cannot use “$function”. Is there any workaround for this?