Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kenmori/5225b33fc6c924a28eb15b8194a5444b to your computer and use it in GitHub Desktop.
Save kenmori/5225b33fc6c924a28eb15b8194a5444b to your computer and use it in GitHub Desktop.
Create an object from an array with multiple ids, using the id as a key and knowing how many identical ids each have

Create an object from an array with multiple ids, using the id as a key and knowing how many identical ids each have

function createOrder(organization: TreeOrganization, valueId: number): number {
    return organization.values
        .map(o => o.parentId)
        .reduce((a, c) => {
            return a[`${c}`] === undefined
                ? { ...a, [`${c}`]: 1 }
                : { ...a, ...{ [`${c}`]: a[`${c}`] + 1 } }
        }, {} as { [ind: string]: number })
}

author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment