[Feature]: How about add `indexBy` function
See original GitHub issuePackage Scope
- Add to an existing package
- New package
Package name: @toss/utils
Overview
A function that indexes an array of objects based on a specific key value. It is a function that makes the object a key and value pair , which makes the cost of find specific data in the future cheaper.
const foo = [
{ id: 1, name: 'park', age: 23 },
{ id: 3, name: 'kim', age: 21 },
{ id: 5, name: 'lee', age: 24 },
];
// normal approach
const result = foo.find(item => item.name === "park") // Tour all items.
const result = foo.find(item => item.name === "kim") // Tour all items.
const result = foo.find(item => item.name === "lee") // Tour all items.
// new indexBy function
const indexedObj = indexBy(foo, "name") // Tour just one tiem
const result = indexedObj["kim"] // quick search
const result = indexedObj["lee"] // quick search
const result = indexedObj["park"] // quick search
If indexed, it is a performance-friendly situation because it can be found immediately without looking for values while traveling all list. These functions are good for those situations when they can be found quickly and easily after a just one traveling.
Describe the solution you’d like
type ElementType<T extends unknown[]> = T[number];
const indexBy = <ArrayObjectType extends Array<Record<PropertyKey, unknown>>, IndexType extends keyof ElementType<ArrayObjectType>>(
arrayObj:ArrayObjectType, key:IndexType
) => {
return arrayObj.reduce((acc, obj) => {
acc[obj[key]] = obj;
return acc
}, {} as any);
}
Additional context
If the developer uses a key which have value that is other than a Property Key, it should be treated well.
Issue Analytics
- State:
- Created 10 months ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Using indexBy() function in UnderscoreJS - YouTube
In this demonstration, we will generate an index for an array of objects.
Read more >Working with Indexed Associations - ORM - Doctrine
You can index your collections by a value in the related entity. This is a first step towards full ordered hashmap support through...
Read more >"indexBy" configuration on associations is not respected by L2C
Hi, when using indexBy on OneToMany association, returned collection is not ... your collections that relies on that feature to use set() instead...
Read more >INDEX function - Microsoft Support
Returns the value of an element in a table or an array, selected by the row and column number indexes. Use the array...
Read more >Excel INDEX function with formula examples - Ablebits
Perhaps there aren't many practical uses of Excel INDEX by itself, but in combination with other functions such as MATCH or COUNTA, ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
There is actually an identical utility function in @toss/utils, namely createMapByKey.
I think we can just use
Map
orSet
, not the new util.