In my project, I am working on a method that is responsible for returning a user object based on the provided id
. The challenge here is that this method needs to be adaptable for future use cases and cannot simply rely on the current structure.
The database I am working with has a key called type
, which allows me to filter out a collection of users
. My task is to create an object where each user's _id
serves as the key, while excluding the _id
and type
from the user object.
I aim to leverage tools like lodash or ES6 features to transform an Array
like:
[
{
"_id": "0e12e661cb50068a135b36067f001d20",
"name": "Joe Bloggs",
"type": "user"
},
{
"_id": "0e12e661cb50068a135b36067f00373f",
"name": "Ben Bloggs",
"type": "user"
}
]
Into an Object
structured as follows, omitting the _id
and type
:
{
"0e12e661cb50068a135b36067f001d20": {
"name": "Joe Bloggs"
},
"0e12e661cb50068a135b36067f00373f": {
"name": "Ben Bloggs"
}
}
UPDATE: It's crucial for me to return the entire object rather than just the name, as additional properties may be added to these objects in the future.