question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Feature]: How about add `indexBy` function

See original GitHub issue

Package 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:closed
  • Created 10 months ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
raon0211commented, Dec 5, 2022

There is actually an identical utility function in @toss/utils, namely createMapByKey.

1reaction
HoseungJangcommented, Dec 2, 2022

I think we can just use Map or Set, not the new util.

const foo = [
  { id: 1, name: 'park', age: 23 },
  { id: 3, name: 'kim', age: 21 },
  { id: 5, name: 'lee', age: 24 },
];
const names = new Set(foo.map(({ name }) => name));

if (name.has('park')) {
  ...
}
const fooByName = new Map(foo.map((e) => [e.name, e]));

const result = fooByName.get('park');
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found