Last active
May 19, 2019 10:22
-
-
Save srph/7d21fbda270c07fb3b6d696bf8f4dc80 to your computer and use it in GitHub Desktop.
JS: Performantly get the original index of an item.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import get from 'lodash.get' | |
interface SearchMap { | |
[key: string]: number | |
} | |
/** | |
* Performantly get the original index of an item. | |
* Use-case: Get index of item in array A in array B | |
* e.g., Check if user (inside users array) is inside the invitations array | |
* | |
* @input | |
* [{ id: 5 }, { id: 6 }, { id: 7 }, { id: 32 }] | |
* @output | |
* { 5: 0, 6: 1, 7: 2, 32: 3 } | |
* | |
* @example | |
* const users = [{ id: 5, name: 'Hello' }] | |
* const selectedUser = users[0] | |
* const invitationMap = toSearchObjectIndex(party.invitations, 'recipient.id') | |
* const userInvitation = party.invitations[users[0]] | |
* | |
* @param array | |
* @param property | |
*/ | |
export default function toSearchIndexObject<T = any>(array: T[], property: string): SearchMap { | |
return array.reduce((prev: SearchMap, current: T, index: number) => { | |
prev[get(current, property)] = index | |
return prev | |
}, {}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment