Closed simoami closed 8 months ago
This is a great suggestion, making it convenient for raw data conversion while also avoiding performance loss. We have made corresponding modifications based on your suggestion, and this feature is supported in version 1.49.9.
Amazing! thank you. The type inference works better too compared to my example.
const myArray = [
{ id: 1, name: 'item 1' },
{ id: 2, name: 'item 2' },
];
const myMap1 = new HashMap(myArray, {
toEntryFn: (entry) => [entry.id, entry],
});
Glad you're satisfied with our development. Remember to star our project. If you're willing, you can also contribute code. Here's our developer Telegram community: https://t.me/+wlFkdjeFoVpiY2M1.
Is your feature request related to a problem? Please describe. a common pattern with Maps in general is wanting to index an array of objects by a particular attribute key of the object.
This pattern often looks like this:
With the approach above, we would end up with dual iterations over the array because you also call
this.setMany(elements)
Describe the solution you'd like An ideal option is to add an optional mappingFn that lets the user customize how to transform the array type to an array to of [key, value]
I had initially thought about adding my own
MapUtils.fromArray()
, but if it's supported by your HashMap, we would just use yours without additional wrappers. Although untested. it could look like the following:Usage would be:
What's your thoughts on this?