How to convert an array of objects into a flat map in JavaScript